<span style="font-size:18px;">/**
*
*在定义类时,定义泛型
*/
public class People <T>{
private T name;
private T age;
public People(){
}
public T getName() {
return name;
}
public void setName(T name) {
this.name = name;
}
public T getAge() {
return age;
}
public void setAge(T age) {
this.age = age;
}
}
</span>调用:
<span style="font-size:18px;">/**
* 定义类时,使用泛型
* 泛型的使用
*
*/
public class FanXing1 {
public static void main(String[] args) {
// 在定义类时使用泛型
People<String>people = new People<String>();
people.setName("刘备");
people.setAge("10");
String nameStr=people.getName();
String ageStr=people.getAge();
// 打印名字
System.out.println(nameStr);
System.out.println(ageStr);
}
}</span>
本文通过一个具体的Java泛型类的定义与使用示例,详细介绍了如何在Java中定义和使用泛型类,包括创建泛型类、设置类型参数、使用泛型方法等基本操作。
定义类时,使用泛型&spm=1001.2101.3001.5002&articleId=51166035&d=1&t=3&u=a29a4190a09d4f0eb74eecb214f3a81f)

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



