Syntax error, insert "Dimensions" to complete ReferenceType

今天在写下面的代码时,编译器报错:Syntax error, insert "Dimensions" to complete ReferenceType

package s1;

public class O {
	public static void main(String[] args) {
		O1<Object> o1 = new O1();
		O1<Integer> o12 = new O1();
                //因为int,报错:Syntax error, insert "Dimensions" to complete ReferenceType
		O1<int> o12 = new O1<int>(); 
	}
}

class O1<T> {
	static void test() {
		
	}
}

上网查找资料发现是因为:class O1<T>,只可以使用引用(对象)类型,不可以是基本类型!

如果是方法void test(T t)则既可以使用引用(对象)类型,也可以使用基本类型。例子:

package s1;

public class O {
	public static void main(String[] args) {
		O1<Object> o1 = new O1();
		o1.test(2);
	}
}

class O1<T> {
	<T> void test(T t) {
		System.out.println("test");
	}
}
发布了81 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/GracefulGuigui/article/details/103950934