How does Java inheritance work when inner classes are involved(涉及内部类时 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)
这篇关于涉及内部类时 Java 继承如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:涉及内部类时 Java 继承如何工作


- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 转换 ldap 日期 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 获取数字的最后一位 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01