公共基类初始化

时间:2012-11-29 05:59:22

标签: c# oop design-patterns inheritance

采取方案

BaseA - > SubB - > SubSubC

说明:SubSubC类继承SubB类。 SubB类继承BaseA类

BaseA - > SubD - > SubSubE

说明:SubSubE类继承SubD类。 SubB类继承BaseA类

So..on ..

所以有许多类将其祖父类作为BaseA。 BaseA类具有一些对所有方法都通用的属性。例如:CustomerID,LastLogin,UniqueName等。

这就是我正在使用的服务中设计类的方法。

我的疑问是在调用服务方法时,所有方法都会期望任何SubSub类的参数。任何人都可以告诉我,如果我可以在一个地方为BaseA的属性赋值,那么在创建SubSub对象时我不需要每次都填充基本属性吗?

我使用C#作为我的语言。

代码:

public class BaseA
    {
        public int CustomerId { get; set; }
        public string UniqueName { get; set; }
    }

    public class SubB : BaseA
    {
    }
    public class SubSubC : SubB
    {

    }

    public class SubD : BaseA
    {
    }
    public class SubSubE : SubD
    {

    }

    public class MyMain
    {
        public void SendRequestToService1()
        {
            (new MyServiceObject()).ServiceMethod1(new SubSubC());
        }

        public void SendRequestToService2()
        {
            (new MyServiceObject()).ServiceMethod2(new SubSubE());
        }
    }

在上面的代码中,在SendRequestToService1和SendRequestToService2中,我需要初始化基类属性CustomerId和UniqueName。 例如:

(new SubSubC(){ CustomerId=2, UniqueName="XXBB" });

如果有很多方法,我需要每次为各自的类初始化这些属性。有没有办法可以在一个地方初始化基本属性CustomerId和UniqueName,以便继承类(SubSubC,SubSubE here)在创建对象时不需要初始化?

3 个答案:

答案 0 :(得分:2)

您是否在询问调用基础构造函数?如果是这样的话:

class SubSubC : SubB
{
    public object CProperty { get; private set; }
    public SubSubC(object cProperty, string bProperty, int id) : base(bProperty, id)
    {
        CProperty = cProperty;
    }
}
class SubB : BaseA
{
    public string BProperty { get; private set; }
    public SubB(string bProperty, int id) : base(id)
    {
        BProperty = bProperty;
    }
}
class BaseA
{
    public int ID { get; private set; }
    public BaseA(int id)
    {
        ID = id;
    }
}

或者您是否在询问如何在方法中初始化对象?如果是这样(假设以下代码中的setter是公共的,与前面的代码不同):

void SetSubSubCProperties(SubSubC c, object cProperty, string bProperty, int id)
{
    c.CProperty = cProperty;
    SetSubBProperties(c, bProperty, id);
}
void SetSubBProperties(SubB b, string bProperty, int id)
{
    b.BProperty = bProperty;
    SetBaseAProperties(b, id);
}
void SetBaseAProperties(BaseA a, int id)
{
    a.ID = id;
}

好的,Alex Filipovici的回答,看起来你想要通过从不同的派生类的实例复制基类属性来初始化派生类的实例。在这种情况下,为了减少Alex答案中的重复,你可以这样做:

void Initialize(BaseA source, BaseA target)
{
    target.CustomerID = source.CustomerID;
    target.UniqueName = source.UniqueName;
}

然后,修改他的例子:

public void SendRequestToService1()
{
    var subSub = new SubSubC();
    Initialize(this.baseA, subSub);
    (new MyServiceObject()).ServiceMethod1(subSub);
}

public void SendRequestToService2()
{
    var subSub = new SubSubE();
    Initialize(this.baseA, subSub);
    (new MyServiceObject()).ServiceMethod2(subSub);
}

答案 1 :(得分:2)

您在寻找以下功能吗?

    //dummy class replacing the service object and it's methods
    public class MyServiceObject
    {
        public void ServiceMethod1(SubSubC param)
        { }
        public void ServiceMethod2(SubSubE param)
        { }
    }

    public class BaseA
    {
        public int CustomerId { get; set; }
        public string UniqueName { get; set; }
    }

    public class SubB : BaseA
    {
    }
    public class SubSubC : SubB
    {
    }

    public class SubD : BaseA
    {
    }
    public class SubSubE : SubD
    {
    }

    public class MyMain
    {
        //declare the SubSub objects
        //SubSubC subSubC;
        //SubSubE subSubE;
        BaseA baseA;

        public MyMain()
        {
            //assign the values to each class in the MyMain contrsuctor
            baseA = new BaseA { CustomerId = 2, UniqueName = "XXBB" };

        }

        public void SendRequestToService1()
        {
            var subSub=new SubSubC();
            (new MyServiceObject()).ServiceMethod1(Initialize(subSub));
        }

        public void SendRequestToService2()
        {
            var subSub = new SubSubE();
            (new MyServiceObject()).ServiceMethod2(Initialize(subSub));
        }

        private T Initialize<T>(T subSub) where T:BaseA
        {
            subSub.CustomerId = baseA.CustomerId;
            subSub.UniqueName = baseA.UniqueName;
            return subSub;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyMain myMain = new MyMain();
            myMain.SendRequestToService1();
            myMain.SendRequestToService2();
        }
    }

答案 2 :(得分:0)

不确定我理解你的问题。

public class A {
  public int ID { get; set; }
}

public class B : A {
}

您可以B b = new B()然后b.ID = 12


或者,如果您的方法在服务中获取类型A的参数,则可以以相同的方式更改值。

public void doSomething(A a) {
  a.ID = 12;
}

并使用B - doSomething(new B())

的实例调用该方法