Java8接口的默認方法
什么是默認方法,為什么要有默認方法?
簡單說,就是接口可以有實現方法,而且不需要實現類去實現其方法。只需在方法名前面加個default關鍵字即可。
為什么要有這個特性?首先,之前的接口是個雙刃劍,好處是面向抽象而不是面向具體編程,缺陷是,當需要修改接口時候,需要修改全部實現該接口的類,目前的 java 8之前的集合框架沒有foreach方法,通常能想到的解決辦法是在JDK里給相關的接口添加新的方法及實現。然而,對于已經發布的版本,是沒法在給接口添加新方法的同時不影響已有的實現。所以引進的默認方法。他們的目的是為了使接口沒有引入與現有的實現不兼容發展。
如以下所示,
1
2
3
4
5
|
public interface Animal { default void eat() { System.out.println( "animal eat default method" ); } } |
聲明了一個接口,里面只有一個默認方法。然后寫一個具體的類實現這個接口,
1
2
3
4
5
6
7
8
9
10
|
public class Dog implements Animal { public void sayHi() { System.out.println( "dog" ); } public static void main(String args[]) { Dog dog = new Dog(); dog.eat(); } } |
再具體的類里面不是必須重寫默認方法,但必須要實現抽象方法。
默認方法的多重繼承
如下所示代碼,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public interface A { void doSomething(); default void hello() { System.out.println( "hello world from interface A" ); } default void foo() { System.out.println( "foo from interface A" ); } } interface B extends A { default void hello() { System.out.println( "hello world from interface B" ); A. super .hello(); this .foo(); A. super .foo(); } } class C implements B, A { @Override public void doSomething() { System.out.println( "c object need do something" ); } public static void main(String args[]) { A obj = new C(); obj.hello(); //調用B的方法 obj.doSomething(); } } |
打印結果:
1
2
3
4
5
6
7
8
9
|
hello world from interface B hello world from interface A foo from interface A foo from interface A c object need do something |
obj.hello()調用的是B接口中的默認方法。同時在B接口中的默認方法有調用了父接口中的默認方法。
我們再來看一個例子,思考一下在多重繼承中,如果出現了同名的默認方法,如下所示,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public interface D { default void hello() { System.out.println( "hello world from D" ); } } interface E { default void hello() { System.out.println( "hello world from E" ); } } class F implements D, E { @Override public void hello() { System.out.println( "hello world F class" ); D. super .hello(); E. super .hello(); } public static void main(String args[]) { F f = new F(); f.hello(); } } |
我們需要制定調用哪個接口的默認方法如下,
1
2
|
D. super .hello(); E. super .hello(); |
另一個java8的接口默認方法實例:
java8新增了接口的默認方法, 也就是說在接口中也可以有實現了, 這個實現方法是默認的實現,你也可以在接口的實現類里對此默認方法進行重寫。
如下實例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public class AppInterfaceDefaultMethod { public static interface DefaultMethodDemo { //定義默認方法, 默認方法前面加default關鍵字, 后面跟方法聲明和方法體 default void demo(String input) { System.out.println(input); } void doSomething(); } public static class DemoClass implements DefaultMethodDemo { @Override public void doSomething() { System.out.println( "do something" ); } } public static class DemoClassOverrideDemo implements DefaultMethodDemo { //重寫了默認方法 @Override public void demo(String input) { System.out.println( "demo " + input + " by override method" ); } @Override public void doSomething() { System.out.println( "do something" ); } } public static void main(String[] args) { DefaultMethodDemo demo = new DemoClass(); demo.demo( "abc" ); DefaultMethodDemo demoOverride = new DemoClassOverrideDemo(); demoOverride.demo( "abc" ); } } |
以上就是關于Java8接口的默認方法詳細介紹,希望對大家的學習有所幫助。