方法引Consumer,Function,UnaryOperator,BiFunction,Supplier
作者:互联网
public class Dog {
private String name;
private int age;
public Dog() {
super();
}
public Dog(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/**
* 狗叫,静态方法
*
*/
public static void bark(Dog dog) {
System.out.println(dog.getName() + "叫了");
}
public String showDogName(Dog dog) {
dog.setName("真" + dog.getName());
System.out.println("狗叫:" + dog.getName());
return dog.getName();
}
public String reName(String dogName) {
System.out.println("reName :" + dogName);
return dogName + "起飞 !";
}
public String getInfo(int age,String name) {
System.out.println("name :" + name);
System.out.println("age :" + age);
return "小狗: 叫" + name +";" + age+"个月大!";
}
@Override
public String toString() {
return "Dog [name=" + name + ", age=" + age + "]";
}
}
public static void main(String[] args) {
// 静态方法引用
Dog dog = new Dog();
dog.setName("哮天犬");
Consumer<Dog> dogCousumer = Dog::bark;
dogCousumer.accept(dog);
Dog dog2 = new Dog();
dog2.setName("真的狗");
// 非静态方法,使用对象实例的方法引用
Function<Dog,String> function = dog::showDogName;
function.apply(dog);
//一元函数
UnaryOperator<String> unary = dog::reName;
unary.apply("狗子");
BiFunction<Integer,String,String> biFun = dog::getInfo;
System.out.println(biFun.apply(18,"土子"));
//构造函数的方法引用
Supplier<Dog> supDog =Dog::new;
BiFunction<String,Integer,Dog> biDog =Dog::new;
System.out.println(biDog.apply("旺财", 23));
}
标签:Function,BiFunction,name,age,dog,Dog,Supplier,public,String 来源: https://blog.csdn.net/create_bj/article/details/121890937