简体   繁体   English

如何制作抽象类并继承其他类

[英]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 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 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. 注意:不要使用类名BaseClass使它更适合于域。 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. 还要注意,如果Source是公共的,则BaseClass也必须是公共的。 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. 如果您真的想在内部烘焙BaseClass,则可以使用封装而不是继承-仍在重用代码,但方法有所不同。

Thanks To D Stanley__ your answer helped me a lot to solve this problem 感谢D Stanley__,您的回答对我解决了很多问题

AbstractClass.cs 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 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 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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从抽象基类多重继承? - How to mulitiply inherit from abstract base classes? C# 是否值得只用一种方法创建抽象 class 并且这些类之间是否足够相关以从一个基础 class 继承? - C# Is it worth to create abstract class with only one method and are this classes enough related to each other to inherit form one base class? 如何编写通用方法来复制均从同一抽象基类继承的类的实例? - How do I write a general method to copy instances of classes that all inherit from the same abstract base class? 如何从可以由其他类继承的类继承并保留其他类属性(对于视图模型) - How to inherit from class that could be inherited by other classes and keep other classes properties (for view model) 从类或抽象类继承 - Inherit from a class or an abstract class 如何从控件类和抽象类继承? - How can I inherit from a control class and an abstract class? 如何使用抽象基类继承内部类? - How can I inherit an inner class using an abstract base class? C# - 如何使方法仅对继承该方法基类的类可见 - C# - How to make a method only visible to classes that inherit the base class of the method 在aspx页面中继承抽象类 - inherit abstract class in aspx page 继承自抽象 class 的用户控件 - Usercontrols that inherit from abstract class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM