[英]Does a nested inner class automatically inherit from superclass of outer class in java?
Say I have an Outer class A and it's nested Inner Class B. A is subclass of C.假设我有一个外部 class A,它是嵌套的内部 Class B。A 是 C 的子类。 Then is B also a subclass of C?
那么B也是C的子类吗? Since B can access all private members of A and thus that(public and protected) of it's superclass, so I think B becomes a subclass of C.
由于 B 可以访问 A 的所有私有成员,因此它的超类的(公共和受保护的),所以我认为 B 成为 C 的子类。
Is my line of thinking right?我的思路对吗?
Any help is highly appreciated.非常感谢任何帮助。
Here's what you seem to be describing:这是您似乎在描述的内容:
class C {}
class A extends C {
static class B {}
}
Being a nested class in A
does not make B
a subclass of C
.作为
A
中的嵌套 class不会使B
成为C
的子类。 Access to members is not the defining characteristic of being a subclass.访问成员不是作为子类的定义特征。
Suppose you try to assign an object of type B
to a variable of type C
.假设您尝试将
B
类型的C
分配给 C 类型的变量。
C c1 = new A(); // OK -- A is a subclass of C
C c2 = new A.B(); // Not OK
If B
were a subclass of C
, the latter would be a legal assignment.如果
B
是C
的子类,则后者将是合法分配。 But it is not.但事实并非如此。
I don't think so.我不这么认为。 If you want B to extend C, you may explicitly say:
如果你想让 B 扩展 C,你可以明确地说:
class C {}
class A extends C {
static class B extends C {
//Todo...
}
}
The nested class B is just a (static) member of A just like any other member of it.嵌套的 class B 只是 A 的(静态)成员,就像它的任何其他成员一样。 You can access public and protected or even package access methods and variables of C within B because A has inherited those members from C.
您可以在 B 中访问 C 的公共和受保护甚至 package 访问方法和变量,因为 A 从 C 继承了这些成员。 Since B is a nested class of A, You can access (static) members of A which can be inherited from other class or not.
由于 B 是 A 的嵌套 class,因此您可以访问 A 的(静态)成员,这些成员可以从其他 class 继承或不继承。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.