返回博主主页

java泛型——同一类型

1.定义一个泛型类

   class Box<T> {
        private T data;
        public Box() {
        }
        public Box(T data) {
            this.data = data;
        }
        public T getData() {
            return data;
        }
    }

2.测试,返回的class相同

@Test(timeout = 100)
    public void useAppContext() {
        Box<String> name = new Box<String>("corn");
        Log.e(TAG, name.getData());
        Box<Integer> age = new Box<Integer>(712);

        Log.e(TAG, name.getClass().toString());      // com.example.androidstudydemo.Box
        Log.e(TAG, age.getClass().toString());        // com.example.androidstudydemo.Box
        Log.e(TAG, (name.getClass() == age.getClass()) + "");    // true

 

posted @ 2021-11-02 12:11  懒惰的星期六  阅读(133)  评论(0编辑  收藏  举报

Welcome to here

主页