简体   繁体   中英

How do I access a private variable from a method?

currentColor = getCarColor(this.car.color)

这里的color是私有的,而getCarColor是一个方法,我如何访问变量color

You should not be accessing private variables directly: they are made private for a reason.

The proper way to do it is to add a public accessor method for the color to the car:

class Car {
    private Color color;
    // Add this method:
    public Color getColor() { return color; }
}

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