Java8接口默認(rèn)靜態(tài)方法及重復(fù)注解原理解析
接口默認(rèn)方法和靜態(tài)方法
默認(rèn)方法
interface MyInterface1 { default String method1() { return "myInterface1 default method"; } } class MyClass{ public String method1() { return "myClass method"; } } /** * 父類和接口中都有相同的方法,默認(rèn)使用父類的方法,即類優(yōu)先 * @author 莫雨朵 * */ class MySubClass1 extends MyClass implements MyInterface1{ } @Test public void test1() { MySubClass1 mySubClass1=new MySubClass1(); System.out.println(mySubClass1.method1());//myClass method }
如果類的父類的方法和接口中方法名字相同且參數(shù)一致,子類還沒有重寫方法,那么默認(rèn)使用父類的方法,即類優(yōu)先
interface MyInterface1 { default String method1() { return "myInterface1 default method"; } } interface MyInterface2 { default String method1() { return "myInterface2 default method"; } } /** * 如果類實(shí)現(xiàn)的接口中有名字相同參數(shù)類型一致的默認(rèn)方法,那么在類中必須重寫 * @author 莫雨朵 * */ class MySubClass2 implements MyInterface1,MyInterface2{ @Override public String method1() { return MyInterface1.super.method1(); } } @Test public void test2() { MySubClass2 mySubClass2=new MySubClass2(); System.out.println(mySubClass2.method1());//myInterface1 default method }
如果類實(shí)現(xiàn)的接口中有名字相同參數(shù)類型一致的默認(rèn)方法,那么在類中必須重寫
靜態(tài)方法
interface MyInterface1 { static String method2() { return "interface static method"; } } @Test public void test3() { System.out.println(MyInterface1.method2());//interface static method }
重復(fù)注解
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MAnnotation { String name() default ""; int age(); } public class AnnotataionTest { @Test public void test() throws Exception { Class<AnnotataionTest> clazz=AnnotataionTest.class; Method method = clazz.getMethod("good", null); MAnnotation annotation = method.getAnnotation(MAnnotation.class); System.out.println(annotation.name()+":"+annotation.age()); } @MAnnotation(name="tom",age=20) public void good() { } }
以前我們是這樣使用注解,當(dāng)要在一個(gè)方法上標(biāo)注兩個(gè)相同的注解時(shí)會(huì)報(bào)錯(cuò),java8允許使用一個(gè)注解來存儲(chǔ)注解,可以實(shí)現(xiàn)一個(gè)注解重復(fù)標(biāo)注
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Repeatable(MAnnotations.class)//使用@Repeatable來標(biāo)注存儲(chǔ)注解的注解 public @interface MAnnotation { String name() default ""; int age(); } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MAnnotations { MAnnotation[] value(); } public class AnnotataionTest { @Test public void test() throws Exception { Class<AnnotataionTest> clazz=AnnotataionTest.class; Method method = clazz.getMethod("good"); MAnnotation[] mAnnotations = method.getAnnotationsByType(MAnnotation.class); for (MAnnotation annotation : mAnnotations) { System.out.println(annotation.name()+":"+annotation.age()); } } @MAnnotation(name="tom",age=20) @MAnnotation(name="jack",age=25) public void good() { } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于SpringBoot實(shí)現(xiàn)代碼在線運(yùn)行工具
這篇文章主要介紹了如何利用SpringBoot實(shí)現(xiàn)簡(jiǎn)單的代碼在線運(yùn)行工具(類似于菜鳥工具),文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06MybatisX無法自動(dòng)生成entity實(shí)體類的解決方法
本文主要介紹了MybatisX無法自動(dòng)生成entity實(shí)體類的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java中synchronized實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Java中synchronized實(shí)現(xiàn)原理詳解,涉及synchronized實(shí)現(xiàn)同步的基礎(chǔ),Java對(duì)象頭,Monitor,Mark Word,鎖優(yōu)化,自旋鎖等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11mybatisplus?@Select注解中拼寫動(dòng)態(tài)sql異常問題的解決
這篇文章主要介紹了mybatisplus?@Select注解中拼寫動(dòng)態(tài)sql異常問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12JAVA?GUI基礎(chǔ)與MouseListener用法
這篇文章主要介紹了JAVA?GUI基礎(chǔ)與MouseListener用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12java list與數(shù)組之間的轉(zhuǎn)換詳細(xì)解析
以下是對(duì)java中l(wèi)ist與數(shù)組之間的轉(zhuǎn)換進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-09-09