生活随笔
收集整理的这篇文章主要介绍了
[Java基础]接口组成(默认方法,静态方法,私有方法)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
默认方法:
代码如下:
package MyInterfacePack;public interface MyInterface {void show1();void show2();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");
showhhh();showhhh02();}default void show2(){System.out
.println("show2");
showhhh();showhhh02();}static void method(){System.out
.println("method1");
showhhh02();}static void method2(){System.out
.println("method2");
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();}}
总结
以上是生活随笔为你收集整理的[Java基础]接口组成(默认方法,静态方法,私有方法)的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。