为什么我不能创建一个具有明显类型的超类类型的对象?

时间:2017-04-07 19:10:33

标签: java getter private-members

为什么我可以在没有getter(colour == ((ColouredPoint) other).colour)的情况下访问具有私有访问权限的变量?以下是我的ColouredPoint类的代码:

public class ColouredPoint extends Point {
    private Colour colour;

    public ColouredPoint(double x, double y, double z, Colour colour) {
        super(x, y, z);
        this.colour = colour;
    }

    @Override
    public String toString() {
        return "(" + super.toString() + ", " + colour + ")";
    }

    @Override
    public boolean equals(Object other) {
        if (other instanceof ColouredPoint) {
            return x == ((ColouredPoint) other).x
                    && y == ((ColouredPoint) other).y
                    && z == ((ColouredPoint) other).z
                    && colour == ((ColouredPoint) other).colour;
        }
        return false;
    }
}

0 个答案:

没有答案