Java8接口的默認(rèn)方法
Java8接口的默認(rèn)方法
什么是默認(rèn)方法,為什么要有默認(rèn)方法?
簡(jiǎn)單說(shuō),就是接口可以有實(shí)現(xiàn)方法,而且不需要實(shí)現(xiàn)類(lèi)去實(shí)現(xiàn)其方法。只需在方法名前面加個(gè)default關(guān)鍵字即可。
為什么要有這個(gè)特性?首先,之前的接口是個(gè)雙刃劍,好處是面向抽象而不是面向具體編程,缺陷是,當(dāng)需要修改接口時(shí)候,需要修改全部實(shí)現(xiàn)該接口的類(lèi),目前的 java 8之前的集合框架沒(méi)有foreach方法,通常能想到的解決辦法是在JDK里給相關(guān)的接口添加新的方法及實(shí)現(xiàn)。然而,對(duì)于已經(jīng)發(fā)布的版本,是沒(méi)法在給接口添加新方法的同時(shí)不影響已有的實(shí)現(xiàn)。所以引進(jìn)的默認(rèn)方法。他們的目的是為了使接口沒(méi)有引入與現(xiàn)有的實(shí)現(xiàn)不兼容發(fā)展。
如以下所示,
public interface Animal {
default void eat() {
System.out.println("animal eat default method");
}
}
聲明了一個(gè)接口,里面只有一個(gè)默認(rèn)方法。然后寫(xiě)一個(gè)具體的類(lèi)實(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();
}
}
再具體的類(lèi)里面不是必須重寫(xiě)默認(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è)賮?lái)看一個(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)方法, 也就是說(shuō)在接口中也可以有實(shí)現(xiàn)了, 這個(gè)實(shí)現(xiàn)方法是默認(rèn)的實(shí)現(xiàn),你也可以在接口的實(shí)現(xiàn)類(lèi)里對(duì)此默認(rèn)方法進(jìn)行重寫(xiě)。
如下實(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 {
//重寫(xiě)了默認(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)文章
Springcloud Stream消息驅(qū)動(dòng)工具使用介紹
SpringCloud Stream由一個(gè)中間件中立的核組成,應(yīng)用通過(guò)SpringCloud Stream插入的input(相當(dāng)于消費(fèi)者consumer,它是從隊(duì)列中接收消息的)和output(相當(dāng)于生產(chǎn)者producer,它是發(fā)送消息到隊(duì)列中的)通道與外界交流2022-09-09
解決spring-cloud-config 多服務(wù)共享公共配置的問(wèn)題
這篇文章主要介紹了解決spring-cloud-config 多服務(wù)共享公共配置的問(wèn)題,本文通過(guò)多種方法給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
spring batch使用reader讀數(shù)據(jù)的內(nèi)存容量問(wèn)題詳解
這篇文章主要介紹了spring batch使用reader讀數(shù)據(jù)的內(nèi)存容量問(wèn)題詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Springboot FeignClient調(diào)用Method has too m
本文主要介紹了Springboot FeignClient微服務(wù)間調(diào)用Method has too many Body parameters 解決,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
使用Easyexcel實(shí)現(xiàn)不同場(chǎng)景的數(shù)據(jù)導(dǎo)出功能
這篇文章主要為大家詳細(xì)介紹了如何在不同場(chǎng)景下使用Easyexcel實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03

