简体   繁体   中英

How To Make Abstract Class And Inherit In Other Classes

I have two clsses with similar code in both but having different variables

ISource.cs

public interface ISource
{
    string AvailConfigPath { get; }

    string AvailVersion { get; }

    IDictionary<string, string> AvailFiles { get; }
}

public class Source : ISource
{
    public string AvailConfigPath
    {
        get
        {
            return @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Source\AvailInfoFile.config";
        }
    }

    private XDocument document = XDocument.Load(@"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Source\AvailInfoFile.config");

    public string AvailVersion
    {
        get
        {
            return document.Root
                         .Element("InfoConfigFile")
                         .Attribute("version").Value.ToString();
        }
    }

    public IDictionary<string, string> AvailFiles
    {
        get
        {
            return document.Root
                         .Element("files")
                         .Elements("file")
                         .ToDictionary(x => x.Attribute("name").Value,
                                       x => x.Attribute("version").Value);
        }
    }
}

ITarget.cs

   public interface ITarget
{
    string LocalConfigPath { get; }

    string LocalVersion { get; }

    IDictionary<string, string> LocalFiles { get; }
}

internal class Target : ITarget
{
    public string LocalConfigPath
    {
        get
        {
            return @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Target\LocalInfoFile.config";
        }
    }

    private XDocument document = XDocument.Load(@"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Target\LocalInfoFile.config");

    public string LocalVersion
    {
        get
        {
            return document.Root
                     .Element("InfoConfigFile")
                     .Attribute("version").Value;
        }
    }

    public IDictionary<string, string> LocalFiles
    {
        get
        {
            return document.Root
                     .Element("files")
                     .Elements("file")
                     .ToDictionary(x => x.Attribute("name").Value,
                                   x => x.Attribute("version").Value);
        }
    }
}

Now i want to make an abstract class and put my common code in that class file and then inherite that code in these both class files

Looks like you should be able to combine them this way:

public interface ITarget
{
    string LocalConfigPath { get; }
    string LocalVersion { get; }
    IDictionary<string, string> LocalFiles { get; }
}

public interface ISource
{
    string AvailConfigPath { get; }
    string AvailVersion { get; }
    IDictionary<string, string> AvailFiles { get; }
}

internal abstract class BaseClass
{
    public virtual string ConfigPath { get; }
    private XDocument document = XDocument.Load(ConfigPath);

    public string Version
    {
        get
        {
            return document.Root
                     .Element("InfoConfigFile")
                     .Attribute("version").Value;
        }
    }

    public IDictionary<string, string> Files
    {
        get
        {
            return document.Root
                     .Element("files")
                     .Elements("file")
                     .ToDictionary(x => x.Attribute("name").Value,
                                   x => x.Attribute("version").Value);
        }
    }
}

internal class Target : BaseClass, ITarget
{
    public override string LocalConfigPath
    {
        get
        {
            return @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Target\LocalInfoFile.config";
        }
    }

    public string LocalVersion
    {
        get { return Version; }
    }

    public IDictionary<string, string> LocalFiles
    {
        get { return Files; }
    }
}

public class Source : BaseClass, ISource
{
    public override string AvailConfigPath
    {
        get
        { return @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Source\AvailInfoFile.config"; }
    }

    public string AvailVersion
    {
        get { return Version; }
    }

    public IDictionary<string, string> AvailFiles
    {
        get { return Files; }
    }
}

Note: Do not use the class name BaseClass - make it something more domain-appropriate. I was just using it to illustrate that is was the "base class"

Also note that if Source is public then BaseClass has to be public as well. If you really want to bake BaseClass internal then you could use encapsulation instead of inheritance - which is still re-using code but the plumbing is different.

Thanks To D Stanley__ your answer helped me a lot to solve this problem

AbstractClass.cs

public abstract class BaseClass
{

    private string _ConfigPath;

    public string ConfigPath
    {
        get { return _ConfigPath; }
        set { _ConfigPath = value; }
    }


    private XDocument _Document = null;

    private XDocument document
    {
        get
        {
            if (_Document == null)
                _Document = XDocument.Load(ConfigPath);

            return _Document;
        }
    }

    public string Version
    {
        get
        {
            return document.Root
                     .Element("InfoConfigFile")
                     .Attribute("version").Value;
        }
    }

    public IDictionary<string, string> Files
    {
        get
        {
            return document.Root
                     .Element("files")
                     .Elements("file")
                     .ToDictionary(x => x.Attribute("name").Value,
                                   x => x.Attribute("version").Value);
        }
    }
}



ISource.cs

 public interface ISource
{
    string ConfigPath { get; }

    string AvailVersion { get; }

    IDictionary<string, string> AvailFiles { get; }
}
public class Source : BaseClass, ISource
{
    public Source()
    {
        ConfigPath = @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Source\AvailInfoFile.config";
    }


    public string AvailVersion
    {
        get
        {
            return Version;
        }
    }

    public IDictionary<string, string> AvailFiles
    {
        get
        {
            return Files;
        }
    }
}



ITarget.cs

public interface ITarget
{
    string ConfigPath { get; }

    string LocalVersion { get; }

    IDictionary<string, string> LocalFiles { get; }
}

internal class Target : BaseClass, ITarget
{
    public Target()
    {
        ConfigPath = @"D:\Mindful\Visual Studio 2012\Projects\AutoUpdator\AutoUpdator\Target\LocalInfoFile.config";
    }
    public string LocalVersion
    {
        get
        {
            return Version;
        }
    }

    public IDictionary<string, string> LocalFiles
    {
        get
        {
            return Files;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM