可以使用 Java 中的泛型来定义学生类,同时在属性 score 上使用泛型类型,以便能够传递不同类型的值,如下所示:
public class Student<T> {
private T score;
public T getScore() {
return score;
}
public void setScore(T score) {
this.score = score;
}
}
在上述代码中,T 表示泛型类型,可以是任何类型,包括 Integer、Double 等。score 属性的类型为 T,这样就可以根据传入的类型来决定属性的实际类型。
例如,如果要创建一个使用 Integer 类型作为 score 的学生对象,可以这样做:
Student<Integer> student = new Student<Integer>();
student.setScore(80);
Integer score = student.getScore();
温馨提示:答案为网友推荐,仅供参考