简体   繁体   中英

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. 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.

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. If both Private And Business CUstomer inherit Customer, that should all work.

Sample code would help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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