1 .Student泛型类 |
package s0222泛型类; |
//使用时确定类型,比如以下这个例子中,学生的成绩的类型组合可以是多种多样的 |
public class Student<T1,T2> { |
|
private T1 javaScore; |
private T2 oracleScore; |
|
|
|
public Student() { |
} |
public Student(T1 javaScore, T2 oracleScore) { |
this .javaScore = javaScore; |
this .oracleScore = oracleScore; |
} |
public T1 getJavaScore() { |
return javaScore; |
} |
public void setJavaScore(T1 javaScore) { |
this .javaScore = javaScore; |
} |
public T2 getOracleScore() { |
return oracleScore; |
} |
public void setOracleScore(T2 oracleScore) { |
this .oracleScore = oracleScore; |
} |
@Override |
public String toString() { |
return "Student [javaScore=" + javaScore + ", oracleScore=" |
+ oracleScore + "]" ; |
} |
|
|
} |
2 .测试类 |
package s0222泛型类; |
public class Test { |
|
public static void main(String [] args) |
{ |
Student<String,Integer> stu1= new Student<String,Integer>( "17" , 3 ); //成绩类型是String 和Integer |
System.out.println(stu1.toString()); |
|
Student<Integer,Double> stu2= new Student<Integer,Double>( 1 , 3.2 ); //成绩类型是Integer和Double |
System.out.println(stu2.toString()); |
|
} |
} |