我必须编写 3 个接受 BaseClass 类型参数的函数,然后返回 DerivedClasses 作为返回值。
Public DerivedClass1 SimpleTest1(BaseClass baseType)
{
DerivedClass1 derivedClass1 = new DerivedClass1();
derivedClass1.item = baseType.item;
derivedClass1.itemGroup = baseType.itemGroup;
return derivedClass1;
}
Public DerivedClass2 SimpleTest2(BaseClass baseType)
{
DerivedClass2 derivedClass2 = new DerivedClass2();
derivedClass2.item = baseType.item;
derivedClass2.itemGroup = baseType.itemGroup;
return derivedClass2;
}
Public DerivedClass3 SimpleTest3(BaseClass baseType)
{
DerivedClass3 derivedClass3 = new DerivedClass3();
derivedClass3.item = baseType.item;
derivedClass3.itemGroup = baseType.itemGroup;
return derivedClass3;
}
3种方法写的代码都是一样的!有没有更好的方法来实现这一点,而无需重复代码?我可以在这里应用特定的设计模式吗?