请查看以下示例。使用类名称与接口名称调用接口类对象之间的区别
public interface Testing {
public void go();
}
public class TestingImplements implements Testing {
@Override
public void go() {
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
Testing throughInterface = new TestingImplements();
TestingImplements directly = new TestingImplements();
}
}
我的问题是, 什么优势劣势&我获得通过throughInterface了直接。一点解释会很有帮助。
Testing throughInterface = new TestingImplements();
代替,
TestingImplements directly = new TestingImplements();
这并不奇怪,这是首选的方法。 –
您展示的两个示例是相同的(复制+粘贴失败?) – DaniEll