[英]Why is an inner class forced to implement its interface methods, even if the outer class has it?
public class Outer{
public void sayHello(){ System.out.println("Hello!");}
public class Inner implements HelloSayers{}
public interface HelloSayers{
public void sayHello();
}
The type Outer.Inner must implement the inherited abstract method HelloSayers.sayHello().
类型Outer.Inner必须实现继承的抽象方法HelloSayers.sayHello()。
But the problem is the inner class should be considered as implementer of the outer methods. 但是问题是内部类应该被视为外部方法的实现者。 Am I wrong?
我错了吗?
Yes, you are wrong. 是的,你错了。
Inner
can access the members of Outer
, but that does not mean it shares those members. Inner
可以访问 Outer
的成员,但这并不意味着它共享那些成员。
Ie every instance of Inner
contains a reference to the corresponding Outer
object ( Outer.this
). 也就是说,每个
Inner
实例都包含对相应Outer
对象( Outer.this
)的引用。 If you access a member of Outer
from Inner
in your java code, the compiler just translates this to a access to the member of the Outer
referenced by the Inner
object. 如果您在Java代码中从“
Inner
访问“ Outer
成员,则编译器会将其转换为对“ Inner
对象所引用的“ Outer
”成员的访问。 The Inner
class does not contain those members. Inner
类不包含那些成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.