如何忽略覆盖
How to Ignore Overriding
先写代码,再提问
class A {
int value() { return 1; } //The value
int getThisValue() { return this.value(); } //gives back the overridden value
int getValue() { return value(); } //this too
int getSuperValue() { return value(); } //for overriding in B
final int getSuperValue2() { return getSuperValue(); }
//Here I get the not-overridden value
//TODO: simplify the process of getting the un-overridden value
}
class B extends A {
@Override
final int getSuperValue() { return super.value(); } //returns the correct value
}
public class Test extends B{
@Override
int value() { return 3; } //overriding the value
public static void main(String[] args) { //output
Test test = new Test();
System.out.println("getValue(): " + test.getValue()); //3
System.out.println("getThisValue(): " + test.getThisValue()); //3
System.out.println("getSuperValue(): " + test.getSuperValue()); //1
System.out.println("getSuperValue2(): " + test.getSuperValue2()); //1
}
}
我有 A
class,我在其中访问 value
.
此 value
被覆盖
-> 我想访问 A
中未覆盖的 value
我的问题:getSuperValue2() 是获得未覆盖 value
的唯一方法还是有其他方法?
我想知道我是否可以通过仅访问我知道的代码来保护我的代码,但让那些想要稍微更改功能的人可以覆盖我的代码
一旦子class开始覆盖,确实没有办法。这是设计使然 - 您不会参考“getValue()
方法的实现方式 class A 的做法”,这是错误的心智模型。您只需引用“getValue()
方法的实现方式与此对象的实现方式”的概念(注意区别:认为 'objects',而不是 'classes')。一旦我们只谈论 'the impl from this object',那么“但我想要未覆盖的值”的想法就不再有任何意义,因此 为什么 你不能这样做。
want to know if I can protect my Code
当然可以。只需标记方法final
!这是一个有时观察到的模式:
public class Parent {
public final void init() {
childInit();
doStuffThatChildClassesCannotStopFromHappening();
}
protected void childInit() {
// does nothing - child classes can override if they wish
}
}
先写代码,再提问
class A {
int value() { return 1; } //The value
int getThisValue() { return this.value(); } //gives back the overridden value
int getValue() { return value(); } //this too
int getSuperValue() { return value(); } //for overriding in B
final int getSuperValue2() { return getSuperValue(); }
//Here I get the not-overridden value
//TODO: simplify the process of getting the un-overridden value
}
class B extends A {
@Override
final int getSuperValue() { return super.value(); } //returns the correct value
}
public class Test extends B{
@Override
int value() { return 3; } //overriding the value
public static void main(String[] args) { //output
Test test = new Test();
System.out.println("getValue(): " + test.getValue()); //3
System.out.println("getThisValue(): " + test.getThisValue()); //3
System.out.println("getSuperValue(): " + test.getSuperValue()); //1
System.out.println("getSuperValue2(): " + test.getSuperValue2()); //1
}
}
我有 A
class,我在其中访问 value
.
此 value
被覆盖
-> 我想访问 A
中未覆盖的 value
我的问题:getSuperValue2() 是获得未覆盖 value
的唯一方法还是有其他方法?
我想知道我是否可以通过仅访问我知道的代码来保护我的代码,但让那些想要稍微更改功能的人可以覆盖我的代码
一旦子class开始覆盖,确实没有办法。这是设计使然 - 您不会参考“getValue()
方法的实现方式 class A 的做法”,这是错误的心智模型。您只需引用“getValue()
方法的实现方式与此对象的实现方式”的概念(注意区别:认为 'objects',而不是 'classes')。一旦我们只谈论 'the impl from this object',那么“但我想要未覆盖的值”的想法就不再有任何意义,因此 为什么 你不能这样做。
want to know if I can protect my Code
当然可以。只需标记方法final
!这是一个有时观察到的模式:
public class Parent {
public final void init() {
childInit();
doStuffThatChildClassesCannotStopFromHappening();
}
protected void childInit() {
// does nothing - child classes can override if they wish
}
}