Java8接口的默認(rèn)方法
Java8接口的默認(rèn)方法
什么是默認(rèn)方法,為什么要有默認(rèn)方法?
簡(jiǎn)單說,就是接口可以有實(shí)現(xiàn)方法,而且不需要實(shí)現(xiàn)類去實(shí)現(xiàn)其方法。只需在方法名前面加個(gè)default關(guān)鍵字即可。
為什么要有這個(gè)特性?首先,之前的接口是個(gè)雙刃劍,好處是面向抽象而不是面向具體編程,缺陷是,當(dāng)需要修改接口時(shí)候,需要修改全部實(shí)現(xiàn)該接口的類,目前的 java 8之前的集合框架沒有foreach方法,通常能想到的解決辦法是在JDK里給相關(guān)的接口添加新的方法及實(shí)現(xiàn)。然而,對(duì)于已經(jīng)發(fā)布的版本,是沒法在給接口添加新方法的同時(shí)不影響已有的實(shí)現(xiàn)。所以引進(jìn)的默認(rèn)方法。他們的目的是為了使接口沒有引入與現(xiàn)有的實(shí)現(xiàn)不兼容發(fā)展。
如以下所示,
public interface Animal {
default void eat() {
System.out.println("animal eat default method");
}
}
聲明了一個(gè)接口,里面只有一個(gè)默認(rèn)方法。然后寫一個(gè)具體的類實(shí)現(xiàn)這個(gè)接口,
public class Dog implements Animal {
public void sayHi() {
System.out.println("dog");
}
public static void main(String args[]) {
Dog dog = new Dog();
dog.eat();
}
}
再具體的類里面不是必須重寫默認(rèn)方法,但必須要實(shí)現(xiàn)抽象方法。
默認(rèn)方法的多重繼承
如下所示代碼,
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();//調(diào)用B的方法
obj.doSomething();
}
}
打印結(jié)果:
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()調(diào)用的是B接口中的默認(rèn)方法。同時(shí)在B接口中的默認(rèn)方法有調(diào)用了父接口中的默認(rèn)方法。
我們?cè)賮砜匆粋€(gè)例子,思考一下在多重繼承中,如果出現(xiàn)了同名的默認(rèn)方法,如下所示,
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();
}
}
我們需要制定調(diào)用哪個(gè)接口的默認(rèn)方法如下,
D.super.hello(); E.super.hello();
另一個(gè)java8的接口默認(rèn)方法實(shí)例:
java8新增了接口的默認(rèn)方法, 也就是說在接口中也可以有實(shí)現(xiàn)了, 這個(gè)實(shí)現(xiàn)方法是默認(rèn)的實(shí)現(xiàn),你也可以在接口的實(shí)現(xiàn)類里對(duì)此默認(rèn)方法進(jìn)行重寫。
如下實(shí)例:
public class AppInterfaceDefaultMethod {
public static interface DefaultMethodDemo {
//定義默認(rèn)方法, 默認(rèn)方法前面加default關(guān)鍵字, 后面跟方法聲明和方法體
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 {
//重寫了默認(rèn)方法
@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");
}
}
以上就是關(guān)于Java8接口的默認(rèn)方法詳細(xì)介紹,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
java實(shí)現(xiàn)在pdf模板的指定位置插入圖片
這篇文章主要為大家詳細(xì)介紹了java如何實(shí)現(xiàn)在pdf模板的指定位置插入圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(40)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
IntelliJ IDEA Java項(xiàng)目手動(dòng)添加依賴 jar 包的方法(圖解)
這篇文章主要介紹了IntelliJ IDEA Java項(xiàng)目手動(dòng)添加依賴 jar 包,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
java json 省市級(jí)聯(lián)實(shí)例代碼
這篇文章介紹了java json 省市級(jí)聯(lián)實(shí)例代碼,有需要的朋友可以參考一下2013-09-09
Java class文件格式之方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java class文件格式之方法的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

