抽象类共享属性,以任何方式将它们放在一个地方?

时间:2015-02-13 04:22:12

标签: c# oop design-patterns

我有两个抽象类,它们具有几个相同的属性。我怎样才能将它们放在一个共同的地方?

public abstract class ClassA
{
     public abstract string PropertyA {get; }
     public abstract string PropertyB {get; }          
     public string PropertyX {get; set;}
     public void MethodA()
     {
         // do something
     }          
}


public abstract class ClassB
{
     public abstract string PropertyA {get; }
     public abstract string PropertyB {get; }          
     public string PropertyY {get; set;}          
     public void MethodB()
     {
         // do something else
     }
}

public class ClassC1 : ClassA
{
     public string PropertyA {get {return "MyString";} }
     public string PropertyB {get{return "MyOtherString";} }          

}

public class ClassC2 : ClassA
{
     public string PropertyA {get {return "MyString2";} }
     public string PropertyB {get{return "MyOtherString2";} }          

}

public class ClassD1 : ClassB
{
     public string PropertyA {get {return "MyString";} }
     public string PropertyB {get{return "MyOtherString";} }          
}

public class ClassD2 : ClassB
{
     public string PropertyA {get {return "MyString2";} }
     public string PropertyB {get{return "MyOtherString2";} }          
}

这是我的情景。既然PropertyA和PropertyB为这两个类返回了相同的值,我想知道是否有任何方法可以重构这些类,所以我没有在抽象/具体类中放置相同的属性。

4 个答案:

答案 0 :(得分:1)

使用

创建一个公共抽象类并从中继承rest
public abstract class ClassCommon
{
     public abstract string PropertyA {get; }
     public abstract string PropertyB {get; }          
}

public abstract class ClassA : ClassCommon
{              
     public string PropertyX {get; set;}          
}


public abstract class ClassB : ClassA
{     public string PropertyY {get; set;}          
}

您可以覆盖属性而不是使用基类属性。

public class ClassC1 : ClassCommon
{
     public override string PropertyA {get {return "MyString";} }
     public override string PropertyB {get{return "MyOtherString";} }              
}

答案 1 :(得分:1)

此解决方案不会从子类中删除字符串属性,但会删除重复的字符串值。

现在可以在“CustomProperties”类中配置它们一次,然后重复使用。

见下文。

赞成合成优于继承

public class CustomProperties
{
    public string PropertyA { get; private set; }
    public string PropertyB { get; private set; }
}

public interface ICustomProperties
{
      string PropertyA { get; }
      string PropertyB { get; }
}

public abstract class ClassA : ICustomProperties
{
    private readonly CustomProperties properties;

    public ClassA(CustomProperties properties)
    {
        this.properties = properties;
    }

    public string PropertyA
    {
        get { return properties.PropertyA; }
    }

    public string PropertyB
    {
        get { return properties.PropertyB; }
    }

    public string PropertyX { get; set; }

    public void MethodA()
    {
        // do something
    }
}

答案 2 :(得分:0)

除了Adil的答案之外,您可以将您的方法设计为抽象或虚拟,并在派生类中覆盖它们吗?

答案 3 :(得分:0)

不确定ClassA和B是否可以来自同一个基础,但如果是,你可以这样:

public class CommonAB
{
    public string PropertyA { get; private set; }
    public string PropertyB { get; private set; }

    public CommonAB(string a, string b)
    {
        PropertyA = a;
        PropertyB = b;
    }
}

public class ClassA : CommonAB
{
    public ClassA(string a, string b)
        : base(a, b)
    {
    }

    public string PropertyX { get; set; }
    public void MethodA()
    {
        // do something
    }
}


public class ClassB : CommonAB
{
    public ClassB(string a, string b)
        : base(a, b)
    {
    }

    public string PropertyY { get; set; }
    public void MethodB()
    {
        // do something else
    }
}

public class ClassC1 : ClassA
{
    public ClassC1()
        : base("MyString", "MyOtherString")
    {
    }
}

public class ClassC2 : ClassA
{
    public ClassC2()
        : base("MyString2", "MyOtherString2")
    {
    }
}

public class ClassD1 : ClassB
{
    public ClassD1()
        : base("MyString", "MyOtherString")
    {
    }
}

public class ClassD2 : ClassB
{
    public ClassD2()
        : base("MyString2", "MyOtherString2")
    {
    }
}