package GmDemo;
public class GmDemo {
/**
* 泛型演示
*/
public static void main(String[] args) {
Shop<Food> food = new Shop<Food>(new Food[] { new Food() });
Shop<Pet> pet = new Shop<Pet>(new Pet[] { new Pet() });
System.out.println(food.buy(0) + " " + pet.buy(0));
}
}
class Food {
@Override
public String toString() {
return "食品";
}
}
class Pet {
@Override
public String toString() {
return "宠物";
}
}
class Shop<P> {// P的类型由调用者指定
P[] products;
public Shop(P[] products) {
this.products = products;
}
P buy(int i) {
return products[i];
}
}
泛型
最新推荐文章于 2023-12-23 19:39:51 发布