了解方法继承
Understanding method inheritance
下面是 JLS 定义方法继承的方式:
A class C inherits from its direct superclass and direct
superinterfaces all abstract and default (§9.4) methods m for which
all of the following are true:
m is a member of the direct superclass or a direct superinterface, D,
of C.
m is public, protected, or declared with package access in the same
package as C.
No method declared in C has a signature that is a subsignature
(§8.4.2) of the signature of m.
No concrete method inherited by C from its direct superclass has a
signature that is a subsignature of the signature of m.
There exists no method m' that is a member of the direct superclass
or a direct superinterface, D', of C (m distinct from m', D distinct
from D'), such that m' from D' overrides the declaration of the method
m.
第 4 个项目符号我不清楚。甚至不可能在同一个 class 中同时声明抽象和非抽象方法。您不能解释一下该规则的实际含义吗?
The 4th bullet is not clear to me.
这意味着您继承了超类的所有方法,您不会自己覆盖它。
It's even not possible to declare both abstract and non-abstract methods in the same class.
什么?
public abstract class AbstractClass {
public abstract void abstractMethod();
public void concreteMethod() {
}
}
C 可能从它的直接超级 class 和它实现的接口继承方法(在 Java 8 中它甚至可以从接口继承默认实现)。
假设 C
扩展 B
并实现 I
.
B 包含一个方法:
public void m (String s)
我包含一个方法(可能带有默认实现):
public void m (String s)
第4点表示C
不会继承I
的m
方法,因为B
的m
方法存在. C
只会继承 B
的 m
方法。
如果你看 similar section in the JLS for Java 7:
8.4.8. Inheritance, Overriding, and Hiding
A class C inherits from its direct superclass and direct
superinterfaces all abstract and non-abstract methods of the
superclass and superinterfaces that are public, protected, or declared
with default access in the same package as C, and are neither
overridden (§8.4.8.1) nor hidden (§8.4.8.2) by a declaration in the
class.
Methods are overridden or hidden on a signature-by-signature basis.
If, for example, a class declares two public methods with the same
name (§8.4.9), and a subclass overrides one of them, the subclass
still inherits the other method.
If the method not inherited is declared in a class, or the method not
inherited is declared in an interface and the new declaration is
abstract, then the new declaration is said to override it.
If the method not inherited is abstract and the new declaration is not
abstract, then the new declaration is said to implement it.
您会注意到没有类似的语言,如果添加有问题的项目符号是为了解决 Java 8.
中引入的默认接口方法,这是有道理的
下面是 JLS 定义方法继承的方式:
A class C inherits from its direct superclass and direct superinterfaces all abstract and default (§9.4) methods m for which all of the following are true:
m is a member of the direct superclass or a direct superinterface, D, of C.
m is public, protected, or declared with package access in the same package as C.
No method declared in C has a signature that is a subsignature (§8.4.2) of the signature of m.
No concrete method inherited by C from its direct superclass has a signature that is a subsignature of the signature of m.
There exists no method m' that is a member of the direct superclass or a direct superinterface, D', of C (m distinct from m', D distinct from D'), such that m' from D' overrides the declaration of the method m.
第 4 个项目符号我不清楚。甚至不可能在同一个 class 中同时声明抽象和非抽象方法。您不能解释一下该规则的实际含义吗?
The 4th bullet is not clear to me.
这意味着您继承了超类的所有方法,您不会自己覆盖它。
It's even not possible to declare both abstract and non-abstract methods in the same class.
什么?
public abstract class AbstractClass {
public abstract void abstractMethod();
public void concreteMethod() {
}
}
C 可能从它的直接超级 class 和它实现的接口继承方法(在 Java 8 中它甚至可以从接口继承默认实现)。
假设 C
扩展 B
并实现 I
.
B 包含一个方法:
public void m (String s)
我包含一个方法(可能带有默认实现):
public void m (String s)
第4点表示C
不会继承I
的m
方法,因为B
的m
方法存在. C
只会继承 B
的 m
方法。
如果你看 similar section in the JLS for Java 7:
8.4.8. Inheritance, Overriding, and Hiding
A class C inherits from its direct superclass and direct superinterfaces all abstract and non-abstract methods of the superclass and superinterfaces that are public, protected, or declared with default access in the same package as C, and are neither overridden (§8.4.8.1) nor hidden (§8.4.8.2) by a declaration in the class.
Methods are overridden or hidden on a signature-by-signature basis.
If, for example, a class declares two public methods with the same name (§8.4.9), and a subclass overrides one of them, the subclass still inherits the other method.
If the method not inherited is declared in a class, or the method not inherited is declared in an interface and the new declaration is abstract, then the new declaration is said to override it.
If the method not inherited is abstract and the new declaration is not abstract, then the new declaration is said to implement it.
您会注意到没有类似的语言,如果添加有问题的项目符号是为了解决 Java 8.
中引入的默认接口方法,这是有道理的