java8 接口调用默认方法_Java8接口里的默认方法特性
在沒(méi)有默認(rèn)方法特性時(shí),當(dāng)你往接口中添加新方法時(shí),接口內(nèi)部所有實(shí)現(xiàn)的類都要?dú)v經(jīng)一些修改,這將導(dǎo)致上千行的代碼修改工作量。為了避免這點(diǎn),Java8引入了默認(rèn)對(duì)象方法,亦即,如果你想要往現(xiàn)存的接口中添加任何功能,你只需添加默認(rèn)方法特性而不會(huì)影響接口實(shí)現(xiàn)形式。那么java 8 接口默認(rèn)方法有哪些特性,跟yjbys小編一起來(lái)來(lái)看看吧!
讓我們看一些例子來(lái)更好的理解它。例如,我聲明了一個(gè)具有打開(kāi)和讀取功能的接口“BookIterface”。接口的類需要實(shí)現(xiàn)打開(kāi)和讀取方法。
package org.smarttechie;
/**
* The interface is intended to open and read. The implementors should implement the methods to open and read.
* @author Siva Prasad Rao Janapati
*
*/
public interface BookInterface {
/**
* The method opens the book
*/
public void openTheBook();
/**
* The method reads the book
*/
public void readTheBook();
}
現(xiàn)在,我們提供上面接口的實(shí)現(xiàn)代碼
package org.smarttechie;
/**
* The JavaBookImpl is the implementation of BookInterface
* @author Siva Prasad Rao Janapati
*
*/
public class JavaBookImpl implements BookInterface {
/**
* This opens the book
*/
@Override
public void openTheBook() {
System.out.println("The Java book is opened");
}
/**
* This reads the book
*/
@Override
public void readTheBook() {
System.out.println("Reading the Java book");
}
}
現(xiàn)在,我們想要給接口提供一個(gè)關(guān)閉功能。如果你直接添加關(guān)閉功能到book接口中,現(xiàn)存的實(shí)現(xiàn)類需要?dú)v經(jīng)一些修改。有了默認(rèn)方法特性后,我們能給book接口直接添加關(guān)閉功能。默認(rèn)方法對(duì)所有實(shí)現(xiàn)都可用。
package org.smarttechie;
/**
* The interface is intended to open and read. The implementors should implement the methods to open and read.
* @author Siva Prasad Rao Janapati
*
*/
public interface BookInterface {
/**
* The method opens the book
*/
public void openTheBook();
/**
* The method reads the book
*/
public void readTheBook();
/**
* The default method implementation
*/
public default void closeTheBook() {
System.out.println("Closting the book");
}
}
package org.smarttechie;
/**
* The JavaBookImpl is the implementation of BookInterface
* @author Siva Prasad Rao Janapati
*
*/
public class JavaBookImpl implements BookInterface {
/**
* This opens the book
*/
@Override
public void openTheBook() {
System.out.println("The Java book is opened");
}
/**
* This reads the book
*/
@Override
public void readTheBook() {
System.out.println("Reading the Java book");
}
public static void main (String[] args) {
BookInterface bookInter = new JavaBookImpl();
//Call the default method declared in BookInterface
bookInter.closeTheBook();
JavaBookImpl book = new JavaBookImpl();
book.closeTheBook();
}
}
下面給出了上述調(diào)用方法的字節(jié)碼。從字節(jié)碼中,我們可以認(rèn)為默認(rèn)方法是一種“虛方法”。
如果你想,你可以重載實(shí)現(xiàn)類中的默認(rèn)方法:
package org.smarttechie;
/**
* The JavaBookImpl is the implementation of BookInterface
* @author Siva Prasad Rao Janapati
*
*/
public class JavaBookImpl implements BookInterface {
/**
* This opens the book
*/
@Override
public void openTheBook() {
System.out.println("The Java book is opened");
}
/**
* This reads the book
*/
@Override
public void readTheBook() {
System.out.println("Reading the Java book");
}
/*
* This closes the book
*/
public void closeTheBook() {
System.out.println("Closing the JAVA book");
}
public static void main (String[] args) {
BookInterface book = new JavaBookImpl();
book.closeTheBook();
}
}
到這會(huì)兒,你可能有一個(gè)疑問(wèn),如果我們實(shí)現(xiàn)了兩個(gè)具有同樣默認(rèn)方法簽名的接口會(huì)怎樣?在那種情況下,調(diào)用實(shí)現(xiàn)會(huì)得到下面的編譯錯(cuò)誤提示。
“用參數(shù)()和()復(fù)制名為closedTheBook的默認(rèn)方法是繼承于TheBookInterface和BookInterface的類型。”
package org.smarttechie;
public interface TechBookInterface {
/**
* The default method implementation
*/
public default void closeTheBook() {
System.out.println("Closing the book");
}
}
package org.smarttechie;
/**
* The JavaBookImpl is the implementation of BookInterface
* @author Siva Prasad Rao Janapati
*
*/
public class JavaBookImpl implements BookInterface, TechBookInterface {
/**
* This opens the book
*/
@Override
public void openTheBook() {
System.out.println("The Java book is opened");
}
/**
* This reads the book
*/
@Override
public void readTheBook() {
System.out.println("Reading the Java book");
}
public static void main (String[] args) {
BookInterface book = new JavaBookImpl();
book.closeTheBook();
}
}
為了避免這個(gè)編譯錯(cuò)誤,我們需要在實(shí)現(xiàn)類中顯式地定義那個(gè)具有同樣簽名的方法。
package org.smarttechie;
/**
* The JavaBookImpl is the implementation of BookInterface
* @author Siva Prasad Rao Janapati
*
*/
public class JavaBookImpl implements BookInterface, TechBookInterface {
/**
* This opens the book
*/
@Override
public void openTheBook() {
System.out.println("The Java book is opened");
}
/**
* This reads the book
*/
@Override
public void readTheBook() {
System.out.println("Reading the Java book");
}
public void closeTheBook() {
System.out.println("Closing the JAVA book");
}
public static void main (String[] args) {
BookInterface book = new JavaBookImpl();
book.closeTheBook();
}
}
總結(jié)
以上是生活随笔為你收集整理的java8 接口调用默认方法_Java8接口里的默认方法特性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 华为手机滑动速度设置_华为手机打字速度慢
- 下一篇: 在java中jvm目录_JVM具体在哪个