简体   繁体   中英

How to get the field of an object, instantiated in the superclass, from a subclass of a subclass

I need to get a field of an object instantiated in a superclass. The problem is that I need to get it from a subclass two levels deep from the superclass, that is, I am in the class SingleChart which extends SingleTable , which itself extends TemplateReport class. TemplateReport instantiates the private Report object. Report has public getters and setters. I want to retrieve the height field of Report . Is there a way of doing this directly from SingleChart to TemplateReport ?

Private instance variables are inherited by the sub-classes.

So if TemplateReport makes a Report object, and Report has getters and setters. You are able to get the Report object from any subclass by call the getter and then asking for the value you need.

TemplateReport would need a public getReport() method so you can access the report instance from the subclass. If you don't want to allow access to that instance then you could have specific methods that allow access:

public int getReportHeight() {

return report.height;
}

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