生活随笔
收集整理的這篇文章主要介紹了
[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基础]接口组成(默认方法,静态方法,私有方法)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。