diff --git a/day10/src/com/inmind/duotai_interface11/Demo11.java b/day10/src/com/inmind/duotai_interface11/Demo11.java new file mode 100644 index 0000000..5890fe5 --- /dev/null +++ b/day10/src/com/inmind/duotai_interface11/Demo11.java @@ -0,0 +1,23 @@ +package com.inmind.duotai_interface11; + +import com.inmind.object_cast10.Animal; +import com.inmind.object_cast10.Cat; + +/* +类的多态 :父类引用指向子类对象 +接口的多态:接口引用指向实现类对象 + */ +public class Demo11 { + public static void main(String[] args) { + MyInterfaceImpl myInterfaceImpl = new MyInterfaceImpl();//普通写法 + Animal animal = new Cat();//向上转型 + //接口引用指向实现类对象 + MyInterface myInterface = new MyInterfaceImpl();//接口的多态(接口的向上转型) + myInterface.method();//编译看左边,运行看右边 + if (myInterface instanceof MyInterfaceImpl){//接口的类型判断 + MyInterfaceImpl impl = (MyInterfaceImpl) myInterface;//接口的向下转型 + impl.method1(); + } + + } +} diff --git a/day10/src/com/inmind/duotai_interface11/MyInterface.java b/day10/src/com/inmind/duotai_interface11/MyInterface.java new file mode 100644 index 0000000..4c0a1c0 --- /dev/null +++ b/day10/src/com/inmind/duotai_interface11/MyInterface.java @@ -0,0 +1,5 @@ +package com.inmind.duotai_interface11; + +public interface MyInterface { + void method(); +} diff --git a/day10/src/com/inmind/duotai_interface11/MyInterfaceImpl.java b/day10/src/com/inmind/duotai_interface11/MyInterfaceImpl.java new file mode 100644 index 0000000..fc309cc --- /dev/null +++ b/day10/src/com/inmind/duotai_interface11/MyInterfaceImpl.java @@ -0,0 +1,12 @@ +package com.inmind.duotai_interface11; + +public class MyInterfaceImpl implements MyInterface{ + @Override + public void method() { + System.out.println("实现类实现了method方法"); + } + + public void method1(){ + System.out.println("实现类1中method1方法"); + } +}