泛型

package com;

class Person<Q>{
    private Q t;
    public Q getT(){
        return t;
    }
    public void setT(Q temp){
        this.t=temp;
    }
    
}
public class Test {
    public static void main(String[] args) {
        Person<String> p = new Person<String>();
        p.setT("hello world");
        System.out.println(p.getT());
        
        Person<Integer> p1 = new Person<Integer>();
        p1.setT(123456);
        System.out.println(p1.getT());
        
        Person p2 = new Person();
        p2.setT(p);
        System.out.println(p2.getT().getClass().getName());
    }
}

你可能感兴趣的:(泛型)