默认方法:
代码如下:
package MyInterfacePack;public interface MyInterface {void show1();void show2();// void show3();public default void show3(){System.out.println("show3");};}
package MyInterfacePack;public class MyInterfaceImplOne implements MyInterface{@Overridepublic void show1() {System.out.println("One show1");}@Overridepublic void show2() {System.out.println("One show2");}@Overridepublic void show3() {System.out.println("One show3");}
}
package MyInterfacePack;public class MyInterfaceImplTwo implements MyInterface{@Overridepublic void show1() {System.out.println("Two show1");}@Overridepublic void show2() {System.out.println("Two show2");}
}
package MyInterfacePack;public class MyInterfaceDemo {public static void main(String[] args){MyInterface my = new MyInterfaceImplOne();my.show1();my.show2();}
}
静态方法:
代码如下:
package InterPack;public interface Flyable {public static void test(){System.out.println("flyable 中的静态方法执行了");}
}
package InterPack;public interface Inter {void show();default void method(){System.out.println("Inter 中的默认方法执行了");}public static void test(){System.out.println("Inter 中的静态方法执行了");}}
package InterPack;public class InterImpl implements Inter,Flyable{@Overridepublic void show() {System.out.println("show方法执行了");}}
package InterPack;public class InterDemo {public static void main(String[] args){Inter i =new InterImpl();i.show();i.method();Inter.test();Flyable.test();}
}
package InterPack02;public interface Inter {default void show(){System.out.println("show1");
// System.out.println("hello");
// System.out.println("world");showhhh();showhhh02();}default void show2(){System.out.println("show2");
// System.out.println("hello");
// System.out.println("world");showhhh();showhhh02();}static void method(){System.out.println("method1");
// System.out.println("hello");
// System.out.println("world");showhhh02();}static void method2(){System.out.println("method2");
// System.out.println("hello");
// System.out.println("world");showhhh02();}private void showhhh(){System.out.println("hello");System.out.println("world");}private static void showhhh02(){System.out.println("hello");System.out.println("world");}}
package InterPack02;public class InterImpl implements Inter{}
package InterPack02;public class InterDemo {public static void main(String[] args){Inter i = new InterImpl();i.show();System.out.println("----------------------");i.show2();System.out.println("-----------------------");Inter.method();System.out.println("------------------------");Inter.method2();}}