首页 > 代码库 > 接口的多态性

接口的多态性

类的对象赋给类所实现的接口的声明(把所实现的接口看做是“父类”):

  1. public class TestInterface {
  2. public static void main(String[] args) {
  3. D d = new D();
  4. TestInterface t = new TestInterface();
  5. t.test1(d);// A a = new D();
  6. }
  7. public void test1(A a) {
  8. }
  9. }
  10. interface A {
  11. int I = 0;
  12. void method();
  13. }
  14. class D implements A {
  15. @Override
  16. public void method() {
  17. }
  18. }

 

接口的多态性