//在接口中使用泛型
public interface Person <T>{
T getName(T name);
}
class Student implements Person<String>{
@Override
public String getName(String name) {
return name;
}
}
public class Test {
public static void main(String[] args) {
Student student=new Student();
String stu=student.getName("小红");
System.out.println(stu);
//*****************************************
Person<String> person=new Student();
String s=person.getName("小兰");
System.out.println(s);
}
}
本文介绍了如何在Java接口中使用泛型,并通过一个具体的`Person`接口和`Student`类的示例,展示了如何重写泛型方法。重点讲解了如何创建`Person<String>`类型的实例并调用getName()方法。
1043

被折叠的 条评论
为什么被折叠?



