[英]Common base class initialization
Take the scenario 采取方案
BaseA -> SubB -> SubSubC BaseA - > SubB - > SubSubC
Explanation: SubSubC class inherits SubB class. Explanation:SubSubC类继承SubB类。 The SubB class inherits BaseA class
SubB类继承BaseA类
BaseA -> SubD -> SubSubE BaseA - > SubD - > SubSubE
Explanation: SubSubE class inherits SubD class. Explanation:SubSubE类继承SubD类。 The SubB class inherits BaseA class
SubB类继承BaseA类
So..on.. 不久..
So there are many class that has its grand parent class as BaseA . 所以有许多类将其祖父类作为BaseA。 The BaseA class has some properties that are common to all the methods.
BaseA类具有一些对所有方法都通用的属性。 ex: CustomerID, LastLogin, UniqueName etc.
例如:CustomerID,LastLogin,UniqueName等。
This is how the class is designed in the service which I'm consuming. 这就是我正在消费的服务中设计类的方法。
My doubt is While calling the service methods, all the methods would expect a parameter of any SubSub class. 我的疑问是在调用服务方法时,所有方法都会期望任何SubSub类的参数。 Can anyone please tell me, is there any way if I could assign values to the properties of the BaseA in one place so that while creating the SubSub objects I did not need to fill the base properties each time?
任何人都可以告诉我,有没有办法,如果我可以在一个地方为BaseA的属性赋值,这样在创建SubSub对象时,我不需要每次都填充基本属性?
I'm using C# as my language. 我用C#作为我的语言。
Code: 码:
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());
}
}
In the above code, in SendRequestToService1 and SendRequestToService2 , i need to initialise the base class properties CustomerId and UniqueName. 在上面的代码中,在SendRequestToService1和SendRequestToService2中,我需要初始化基类属性CustomerId和UniqueName。 Ex:
例如:
(new SubSubC(){ CustomerId=2, UniqueName="XXBB" });
If there are many methods, I need to initialize these properties each time for their respective classes. 如果有很多方法,我需要每次为它们各自的类初始化这些属性。 Is there a way I can initialize the base properties CustomerId and UniqueName in one place so that the inheriting classes(SubSubC,SubSubE here) no need to initialize when their objects are created?
有没有办法在一个地方初始化基本属性CustomerId和UniqueName,这样继承类(SubSubC,SubSubE here)在创建对象时不需要初始化?
Are you asking about calling base constructors? 你在询问调用基础构造函数吗? If so:
如果是这样的话:
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;
}
}
Or are you asking about initializing objects in a method? 或者您是否在询问如何在方法中初始化对象? If so (assume the setters are public in the following code, unlike in the preceding):
如果是这样(假设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;
}
Ok, Alex Filipovici's answer, it looks like you want to initialize an instance of a derived class by copying the base class properties from an instance of a different derived class. 好的,Alex Filipovici的回答,看起来你想要通过从不同派生类的实例复制基类属性来初始化派生类的实例。 In that case, to reduce the duplication in Alex's answer, you can do this:
在这种情况下,为了减少Alex答案中的重复,您可以这样做:
void Initialize(BaseA source, BaseA target)
{
target.CustomerID = source.CustomerID;
target.UniqueName = source.UniqueName;
}
Then, to modify his example: 然后,修改他的例子:
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);
}
Are you looking for the following functionality? 您在寻找以下功能吗?
//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();
}
}
Not sure I understand your question. 不确定我理解你的问题。
public class A {
public int ID { get; set; }
}
public class B : A {
}
you can do B b = new B()
and then b.ID = 12
. 你可以做
B b = new B()
然后b.ID = 12
。
Or, if you have a method that gets a parameter of type A
in your service you can change the value in the same way. 或者,如果您的方法在服务中获取类型
A
的参数,则可以以相同的方式更改值。
public void doSomething(A a) {
a.ID = 12;
}
and call the method with instances of B
- doSomething(new B())
并使用
B
- doSomething(new B())
实例调用该方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.