package test;
public class Fanxingtest5 {
public static void main(String[] args) {
FruitInter<Fruit> fi = Fruit::new; //获取Fruit 类构造方法的引用
Fruit fruit = fi.Fruit("apple", "red"); //Java 编译器自动根据fi.Fruit()方法的签名,选择合适的构造函数
FruitInter<Fruit> s1 = Fruit::new;
Fruit fruit1 = s1.Fruit("banana", "yellow");
}
}
class Fruit {
private String name;
private String color;
public Fruit(String name, String color) {//构造方法
super();
this.name = name;
this.color = color;
System.out.println("name = " + name + ",color = " + color);
}
public Fruit() {
super();
}
}
interface FruitInter<f extends Fruit> {//泛型类f是Fruit的子类
// 在接口中定义抽象方法Fruit
f Fruit(String name, String color);
}
运行结果:
name = apple,color = red
name = banana,color = yellow