public class General<T> {
    private T item;
    public General(){
        System.out.println("构造General对象");
    }
    public T getItem(){
        return this.item;
    }
    public void setItem(T item){
        this.item = item;
    }
}
public class Main {

    public static void main(String[] args) {
        General<String> str = new General<>();
        str.setItem("hello world");
        System.out.println("这是字符串:"+str.getItem());
        General<Integer> num = new General<>();
        num.setItem(467);
        System.out.println("这是数字:"+num.getItem());
    }
}

java-泛型例子

相关文章:

  • 2022-12-23
  • 2021-10-11
  • 2021-09-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-27
猜你喜欢
  • 2022-12-23
  • 2021-10-18
  • 2022-12-23
  • 2021-12-09
  • 2022-02-07
  • 2021-07-28
  • 2021-09-05
相关资源
相似解决方案