如何仅使用基类中的代码返回派生类? [英] How to return a derived class using only code in the base class?

查看:121
本文介绍了如何仅使用基类中的代码返回派生类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近提出了这个编程问题,但未能解决它。我被要求编写一个可继承的子类,以便下面的代码可以正常工作。这是catch,基类(我将它命名为Party)是唯一一个可以包含Find()的代码的类。

  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 and Company 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() and Company.Find() call Party.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 as Find<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 the Find 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆