方法引用_通过super引用父类的成员方法
生活随笔
收集整理的這篇文章主要介紹了
方法引用_通过super引用父类的成员方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
package com.learn.demo07.SuperMethodReference;
/*定義見面的函數(shù)式接口*/
@FunctionalInterface
public interface Greetable {//定義一個見面的方法void greet();
}
package com.learn.demo07.SuperMethodReference;
/*定義父類*/
public class Human {//定義一個sayHello的方法public void sayHello(){System.out.println("Hello 我是Human!");}
}
package com.learn.demo07.SuperMethodReference;
/*定義子類*/
public class Man extends Human{//子類重寫父類sayHello的方法@Overridepublic void sayHello() {System.out.println("Hello 我是Man!");}//定義一個方法參數(shù)傳遞Greetable接口public void method(Greetable g){g.greet();}public void show(){//調(diào)用method方法,方法的參數(shù)Greetable是一個函數(shù)式接口,所以可以傳遞Lambda/*method(()->{//創(chuàng)建父類Human對象Human h = new Human();//調(diào)用父類的sayHello方法h.sayHello();});*///因為有子父類關(guān)系,所以存在的一個關(guān)鍵字super,代表父類,所以我們可以直接使用super調(diào)用父類的成員方法/* method(()->{super.sayHello();});*//*使用super引用類的成員方法super是已經(jīng)存在的父類的成員方法sayHello也是已經(jīng)存在的所以我們可以直接使用super引用父類的成員方法*/method(super::sayHello);}public static void main(String[] args) {new Man().show();}
}
?
總結(jié)
以上是生活随笔為你收集整理的方法引用_通过super引用父类的成员方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 方法引用_通过类名引用静态成员方法
- 下一篇: 枚举结构定义