我有一个基本的类型组成:
class A{
public A(B1 b1, B2 b2){
this.b1 = b1;
this.b2 = b2;
}
public B1 b1 {get; private set;}
public B2 b2 {get; private set;}
}
class B1{
public B1(C1 c1, C2 c2, C3 c3){
this.c1 = c1;
this.c2 = c2;
this.c3 = c3;
}
public C1 c1 {get; private set;}
public C2 c2 {get; private set;}
public C3 c3 {get; private set;}
}
class B2{
public B2(C1 c1, C2 c2, C3 c3){
this.c1 = c1;
this.c2 = c2;
this.c3 = c3;
}
public C1 c1 {get; private set;}
public C2 c2 {get; private set;}
public C3 c3 {get; private set;}
}
class C1{}
class C2{}
class C3{}
有没有更清洁的构造方法A
?
public static void Main()
{
A a = new A(new B1(new C1(), new C2(), new C3()), new B2(new C1(), new C2(), new C3()));
}
我可以不花new
太多时间就能达到相同的构造吗?
我可以对我的构造函数做些什么使它成为可能?
如果在默认构造函数中A
创建了new B
,并且在默认构造函数中B
创建了C,则可以将其简化为A a = new A();
class A{
public A() : this(new B1(), new B2())
{
}
public A(B1 b1, B2 b2){
this.b1 = b1;
this.b2 = b2;
}
public B1 b1 {get; private set;}
public B2 b2 {get; private set;}
}
class B1{
public B1() : this(new C1(), new C2(), new C3())
{
}
public B1(C1 c1, C2 c2, C3 c3){
this.c1 = c1;
this.c2 = c2;
this.c3 = c3;
}
public C1 c1 {get; private set;}
public C2 c2 {get; private set;}
public C3 c3 {get; private set;}
}
class B2{
public B2() : this(new C1(), new C2(), new C3())
{
}
public B2(C1 c1, C2 c2, C3 c3){
this.c1 = c1;
this.c2 = c2;
this.c3 = c3;
}
public C1 c1 {get; private set;}
public C2 c2 {get; private set;}
public C3 c3 {get; private set;}
}
class C1{}
class C2{}
class C3{}
public static void Main()
{
A a = new A();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句