简体   繁体   English

同一属性的不同子类

[英]Different subclasses for same attribute

The example situation is as follows: I have a shop that rents items. 情况示例如下:我有一家商店在租赁物品。 An item (object of class Item) may be rent by PrivateCustomer or BusinessCustomer, which are both subclasses of Customer. 一个项目(Item类的对象)可以由PrivateCustomer或BusinessCustomer租用,这两个都是Customer的子类。 An item has a String name and a reference to a customer object which rents it. 物品具有字符串名称和对租用该物品的客户对象的引用。

My problem is: How can I make it possible that both private and businness customer are accepted? 我的问题是:如何使私人客户和商务客户都被接受? Just using the Customer superclass (Giving the Item class the attribute private Customer rentBy) didn't seem to work, it doesn't accept a PrivateCustomer in the constructor then. 仅仅使用Customer超类(将Item类赋予属性private Customer rentBy属性)似乎不起作用,因此它在构造函数中不接受PrivateCustomer。

I know this is a very basic question, but I'm apparently searching with the wrong keywords as I can't find an answer. 我知道这是一个非常基本的问题,但是由于找不到答案,因此我显然在搜索错误的关键字。

Make sure in your code you are not using any PrivateCustomer or BusinessCustomer attributes, just Customer. 确保在代码中没有使用任何PrivateCustomer或BusinessCustomer属性,仅使用Customer。 If both Private And Business CUstomer inherit Customer, that should all work. 如果私人和商业客户都继承了客户,那么这一切都应该起作用。

Sample code would help. 示例代码会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM