我将尝试创建一个类似于这样的类系统:
私有访问修饰符对我没有帮助,因为它不会使该方法在Matrix类以外可用(除了它的继承者)。
有什么好的想法吗?
public class Matrix {
private int[][] m;
public Matrix(int rows, int cols) {
//constructor
}
public int get(int row, int col) {
return m[row][col];
}
}
public class Vector extends Matrix {
public Vector() {
//constructor
}
public int get(int index) {
return super.get(0, index);
}
}
我希望Matrix.get(row, col)函数是公共的,但我不希望通过Vector类使其公共化。我不想让这种情况发生:
Vector v = new Vector();
int x = v.get(1, 1);
私有访问修饰符对我没有帮助,因为它不会使该方法在Matrix类以外可用(除了它的继承者)。
有什么好的想法吗?
Vector
不应该扩展Matrix
。你为什么想要实现这样的东西?听起来像是一个XY问题。 - RaminSVector
类中的get
方法并将其标记为过时。@Override @Deprecated /** * 不要使用该方法 */ public int get(int row, int col) { return -1; }
- hanumant