关于私有属性不能被继承中,Java官方解释

来源:互联网 发布:开淘宝店怎么发货 编辑:程序博客网 时间:2024/05/21 17:00

先看看官方给出的代码和解释:
class Point {    int x, y;    void move(int dx, int dy) {        x += dx; y += dy; totalMoves++;    }    private static int totalMoves;    void printMoves() { System.out.println(totalMoves); }}class Point3d extends Point {    int z;    void move(int dx, int dy, int dz) {        super.move(dx, dy); z += dz; totalMoves++; // error    }}

原文解释:Here, the class variable totalMoves can be used only within the class Point; it is not inherited by the subclass Point3d. A compile-time error occurs because method move of class Point3d tries to increment totalMoves.

本人对原文的理解:类变量 totalMoves  只能在 Point 中使用,不能由子类Point3d 继承,错误的发生只要是由于在Point3d 中move 方法试图对类变量 totalMoves 进行自加运算。

阅读全文
0 0