2014年4月17日 星期四

Generics : 多個泛型類型

class GenericNumbers <T1,T2>{
 private T1 number1;
 private T2 number2;
 
 public void setNumber1(T1 number){
  this.number1 = number;
 }
 
 public T1 getNumber1(){
  return number1;
 }
 
 public void setNumber2(T2 number){
  this.number2 = number;
 }
 
 public T2 getNumber2(){
  return number2;
 }
}
public class GenericsMultiDefined {

 public static void main(String[] args) {
  GenericNumbers <Integer, Double> number = new GenericNumbers<Integer, Double> ();
  number.setNumber1(10);
  number.setNumber2(20.5);
  Double d = number.getNumber1() + number.getNumber2();
  System.out.println("total : "+d);
 }

}
執行結果

沒有留言:

張貼留言