简体   繁体   English

其方法签名引用实现该接口的抽象 class 的接口

[英]Interface whose methods signature refer to an abstract class implementing that interface

I am not at all a JAVA expert but I've found that the following code snippets run.我根本不是 JAVA 专家,但我发现以下代码片段运行。

interface Arithmetic {
    MyNumber somma(MyNumber b);
    MyNumber sottrai(MyNumber b);
    MyNumber moltiplica(MyNumber b);
    MyNumber dividi(MyNumber b);
}

abstract class MyNumber implements Arithmetic{
}

public class Test{
    public static void main(String[] args)
    {
        System.out.println("It works");
    }
}

Now in C++ you can do similar things if you do a forward declaration, in Java there's no such thing (to my knowledge) so I don't understand how can Arithmetic be compiled fine despite my MyNumber is actually declared after arithmetic.现在在 C++ 中,如果你进行前向声明,你可以做类似的事情,在 Java 中没有这样的事情(据我所知)所以我不明白如何才能很好地编译算术,尽管我的MyNumber实际上是在算术之后声明的。 Is there a technicality of Java explaining this?是否有 Java 的技术性解释这一点?

Java compilation occurs in multiple phases, one of these phases finds all of the declared class symbols and a later phase fills in all the classes' members (except for the already found nested class symbols). Java 编译发生在多个阶段,其中一个阶段查找所有声明的 class 符号,稍后阶段填充所有类的成员(除了已经找到的嵌套 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6DZ 符号)。

You can read about it here: https://github.com/openjdk/jdk/blob/master/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Enter.java你可以在这里阅读: https://github.com/openjdk/jdk/blob/master/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Enter.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM