Skip to content

Instantly share code, notes, and snippets.

@jingjiecb
Created May 7, 2023 09:13
Show Gist options
  • Save jingjiecb/b8e109a557a042f7d335d66dad2ec552 to your computer and use it in GitHub Desktop.
Save jingjiecb/b8e109a557a042f7d335d66dad2ec552 to your computer and use it in GitHub Desktop.
函数式接口示例
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