涉及内部类时 Java 继承如何工作
当存在内部类时,我无法理解 Java 中的继承是如何工作的.我目前正在研究子类需要稍微改变其父类的内部类的功能.我在下面提出了一个更简单的类比示例.
I am having trouble understanding how inheritance works in Java when inner classes are present. I'm currently working on something where a child class needs to slightly change the functionality of the inner class of it's parent. I've come up with an simpler, analagous example below.
我希望这段代码打印我是 ChildClass.InnerClass",但它却打印了我是 ParentClass.InnerClass".为什么是这样?另外,如果我更改 main 中的 obj 对象为 ChildClass 类型,然后输出更改为我是 ChildClass.InnerClass".这是为什么呢?
I expected this code to print "I am a ChildClass.InnerClass" but instead it prints "I am a ParentClass.InnerClass". Why is this? Also, if I change the obj object in main to be of type ChildClass then the output changes to "I am a ChildClass.InnerClass". Why is this?
一般来说,改变对象的父类内部对象的行为的推荐方法是什么?
In general, what is the recommended way of altering the behavior of an object's parent class's inner object?
class InnerClassTest {
//-----------------------------------------------------------------------
// PARENT CLASS
class ParentClass {
public ParentClass() {
x = new InnerClass();
}
InnerClass x;
class InnerClass {
public void speak() {
System.out.println("I am a ParentClass.InnerClass");
}
}
}
//-----------------------------------------------------------------------
// CHILD CLASS
class ChildClass extends ParentClass {
public ChildClass() {
x = new InnerClass();
}
InnerClass x;
class InnerClass extends ParentClass.InnerClass {
public void speak() {
System.out.println("I am a ChildClass.InnerClass");
}
}
}
//-----------------------------------------------------------------------
// MAIN
public static void main(String[] args) {
ParentClass obj = (new InnerClassTest()).new ChildClass();
obj.x.speak();
}
}
推荐答案
变量不像方法那样被覆盖".
Variable are not "overriden" as methods are.
在您的通话中,您希望 x
是 Child
的,但这不是因为 x
是一个变量,而不是一种方法.
In your call, you expected x
to be the Child
's one but it isn't because x
is a variable, not a method.
但是注意:你的引用类型是ParentClass
所以obj.x
指向ParentClass
的InnerClass
属性,即使 parentClass
后面的真实实例是 ChildClass
!
But pay attention: Your reference type is ParentClass
so obj.x
points to the ParentClass
's InnerClass
attribute even though the real instance behind parentClass
is a ChildClass
!
为了显示您的预期句子,您必须将类型引用更改为 ChildClass
:
In order to display your expected sentence, you have to change the type reference to ChildClass
:
public static void main(String[] args) {
ChildClass obj = (new InnerClassTest()).new ChildClass();
obj.x.speak();
}
为了更好地理解这个概念,尝试在 ParentClass
和 ChildClass
类中定义一个方法:
To better understand the concept, try to define a method in both ParentClass
and ChildClass
classes:
public InnerClass getInnerClass(){
return x;
}
并将 x
设为私有.
因此覆盖概念"适用.
在这种情况下,您的最终电话将是:
Your final call would be in this case:
ParentClass obj = (new InnerClassTest()).new ChildClass();
obj.getInnerClass().speak();
要改变内部类的行为,可以考虑模板方法或更好的模式:策略模式(因为更尊重 DIP)
To alter the behavior of the inner classes, think of Template method pattern or better: Strategy pattern (since more respectful of DIP)
相关文章