[Java] 泛型类 →→→→→进入此内容的聊天室

来自 , 2020-02-24, 写在 Java, 查看 168 次.
URL http://www.code666.cn/view/dfd7468a
  1. 1.Student泛型类
  2. package s0222泛型类;
  3. //使用时确定类型,比如以下这个例子中,学生的成绩的类型组合可以是多种多样的
  4. public class Student<T1,T2> {
  5.        
  6.         private T1  javaScore;
  7.         private T2 oracleScore;
  8.        
  9.        
  10.        
  11.         public Student() {
  12.                 }
  13.  
  14.                 public Student(T1 javaScore, T2 oracleScore) {
  15.                         this.javaScore = javaScore;
  16.                         this.oracleScore = oracleScore;
  17.                 }
  18.  
  19.                 public T1 getJavaScore() {
  20.                         return javaScore;
  21.                 }
  22.  
  23.  
  24.                 public void setJavaScore(T1 javaScore) {
  25.                         this.javaScore = javaScore;
  26.                 }
  27.  
  28.  
  29.                 public T2 getOracleScore() {
  30.                         return oracleScore;
  31.                 }
  32.  
  33.  
  34.                 public void setOracleScore(T2 oracleScore) {
  35.                         this.oracleScore = oracleScore;
  36.                 }
  37.  
  38.                 @Override
  39.                 public String toString() {
  40.                         return "Student [javaScore=" + javaScore + ", oracleScore="
  41.                                         + oracleScore + "]";
  42.                 }
  43.                
  44.                
  45. }
  46.  
  47. 2.测试类
  48. package s0222泛型类;
  49.  
  50. public class Test {
  51.        
  52.         public static void  main(String [] args)
  53.     {
  54.                 Student<String,Integer> stu1=new Student<String,Integer>("17",3); //成绩类型是String 和Integer
  55.                 System.out.println(stu1.toString());
  56.                
  57.                 Student<Integer,Double> stu2=new Student<Integer,Double>(1,3.2);  //成绩类型是Integer和Double
  58.                 System.out.println(stu2.toString());
  59.                
  60.     }
  61. }
  62.  
  63.  

回复 "泛型类"

这儿你可以回复上面这条便签

captcha