Created
May 7, 2023 09:13
-
-
Save jingjiecb/b8e109a557a042f7d335d66dad2ec552 to your computer and use it in GitHub Desktop.
函数式接口示例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
/** | |
* @author claws | |
* @since 2023/5/7 | |
*/ | |
public class InterfaceTester { | |
public static void main(String[] args) { | |
Printer printer = new Printer(); | |
Person claws = new Person("claws"); | |
printer.printHelloMsg(claws::sayHello); | |
} | |
} | |
interface HelloSayer { | |
String sayHello(); | |
} | |
class Person { | |
String name; | |
Person(String name) { | |
this.name = name; | |
} | |
String sayHello() { | |
return "你好!"; | |
} | |
String introduce() { | |
return "我是 " + name; | |
} | |
} | |
class Printer { | |
void printHelloMsg(HelloSayer helloSayer) { | |
String s = helloSayer.sayHello(); | |
System.out.println(s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment