如何仅使用基类中的代码返回派生类? [英] How to return a derived class using only code in the base class?
问题描述
var address = new Address(56 Main St,Town,AZ,12345);
var customer = new Customer(John,Doe,address);
var company = new Company(Town Company,地址);
Customer savedCustomer = Customer.Find(customer.Id);
公司savedCompany = Company.Find(company.Id);
派对
隐藏。 客户
和公司
都会继承它。
当我从其中一个类中调用时,我导致Party返回客户和公司?我被告知可能使用泛型,但我无法将调用更改为 Find< Company>(customer.Id)
。然而,我所检查的所有资源都说,你不能从基类返回派生类型。编辑:我通过 Customer解决了它。 Find()
和 Company.Find()
调用 Party.partyFind()
然后将结果投射到他们的类型。我提交了解决方案以及解释:
我试图将基本类中的Find功能保留在
基类,但我不确定如何从没有泛型的单个基类返回派生的
类型(无论如何需要
修改测试代码)
blockquote>
我收到回复:
你错了,泛型可以解决这个问题无需修改
测试代码;在呼叫站点Find< Customer> 的特殊功能 查找
不允许
。
解决方案只要看一下你的更新,那个人正在谈论的可能是专门为
派对
:
公共类派对< T> ;
{
public static T Find(int id)
{
...
}
}
公共类Customer :Party< Customer> {}
public class Company:Party< Company> {}
这可以确实在派生类上静态调用
Find
,而无需明确地将泛型类型传递给Find
方法。var cust = Customer.Find(1); //返回Customer
的实例var comp = Company.Find(2); //返回公司的实例
现场示例: http://rextester.com/GPDFI94766
I was recently posed this programming problem and failed to solve it. I was asked to write a subclass that could be inherited so that the below code could function correctly. Here's the catch, the base class (I named it Party) is the only one that can contain code for Find().
var address = new Address("56 Main St", "Town", "AZ", "12345"); var customer = new Customer("John", "Doe", address); var company = new Company("Town Company", address); Customer savedCustomer = Customer.Find(customer.Id); Company savedCompany = Company.Find(company.Id);
Party
is hidden.Customer
andCompany
both inherit it.How could I have caused Party to return Customer and Company when called from one of those classes? I was told it's possible using generics but that I can't change the calls to look like
Find<Company>(customer.Id)
. However, all the resources I examined say you can't return derived types from a base class.EDIT: I solved it by have
Customer.Find()
andCompany.Find()
callParty.partyFind()
and then cast the result to their type. I submitted the solution along with the explanation:I tried to keep the Find functionality in the base class as much in the base class as possible but I wasn't sure how to return derived types from a single base class without generics (which would require modifying the test code anyway)
I received the reply:
You're mistaken, generics can solve this problem without modifying the test code; specializing
Find
at the call site asFind<Customer>
is not allowed. You have more to learn about generics.
解决方案Just looking at your update, what the person was talking about was probably specializing
Party
:public class Party<T> { public static T Find(int id) { ... } } public class Customer : Party<Customer>{} public class Company : Party<Company>{}
This can then indeed call
Find
statically on derived classes without passing the generic type to theFind
method explicitly.var cust = Customer.Find(1); // returns instance of Customer var comp = Company.Find(2); // returns instance of Company
Live example: http://rextester.com/GPDFI94766
这篇关于如何仅使用基类中的代码返回派生类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!