简体   繁体   中英

Java Parent instantiated through Child class Parent attribute private set in child constructor

I would like to create an abstract parent class ,which contains the method public abstract use(); and the use method is different in different child classes, but utilize the parent method for all the rest however I would like the parent class to contain a private variable but it needs to be set from the specific child depending on the child so like I have a Parent Car and I want to work with cars in general but, for the specific purpose of the use() method and setting the car image I use the Porsche class which is a child. I dont want the Car image attribute to be accesible but at the same time if its private I cant set it from the child constructor, any ideas? My question is how to set a parent private variable from the child constructor when I instantiate like Parent parent = new Child ();

非常感谢您,保护它,我节省了大量时间,也感谢您的知识:)完美地工作

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