没有类型参数的Java通用类实例化 [英] Java Generic Class Instantiation without Type Argument
问题描述
在下面的代码中,如果我将Generic实例化为:
In the code below, if I instantiate Generic as:
Generic gen=new Generic(1,2);
没有类型参数,那么当我这样做时:
that is without type argument,then when i do this:
int a=gen.get_a();
它不起作用并给出
必填:int找到:Java.Lang.Object
required:int Found:Java.Lang.Object
但是 ob.print()
可以工作.因此,当我改为这样做时:
but ob.print()
works.
So when I do this instead:
int a=(Integer)gen.get_a();
然后它起作用.那么,由于没有传递类型参数时, T
不能是原始的,因此擦除操作是否将 T
替换为 Object
类型?
then it works. So does the erasure replace T
with Object
type since T
cannot be primitive, when no type argument is passed?
public class Generic<T>
{
T a;
Generic(T a)
{
this.a=a;
}
void print()
{
System.out.print(a);
}
T get_a()
{
return a;
}
}
推荐答案
在这里,正如Jon Skeet所说,您在变量声明中使用的是原始类型.
Here, as Jon Skeet said, you are using a raw type in your variable declaration.
Generic gen=new Generic(1,2);
int a=gen.get_a();
它不起作用并给出
it does not work and gives
required:int Found:Java.Lang.Object
如果在声明变量时未指定类型,则编译器将无法猜测类型.
The compiler cannot guess the type if you don't specify it when you declare the variable.
由于T不能为原始,没有传递任何类型参数时?
So does the erasure replace T with Object type since T cannot be primitive, when no type argument is passed?
使用类型要求在声明中指定类.而且原始不是类. Generic< int>gen = new Generic(1);
将不会编译
Using types demands specifying class in the declaration. And a primitive is not a class.
Generic<int> gen = new Generic<>(1);
will not compile
因此,如果要使用整数值键入实例,则必须指定int原语的包装对象: Generic< Integer>gen = new Generic(1);
当您声明一个依赖于数字类型的泛型的集合变量时,您一定已经注意到它了.
So, you have to specify the wrapper object of int primitive if you want to type your instance with an integer value :
Generic<Integer> gen = new Generic<>(1);
You must have done noticed it when you declare a collection variable with generics relying on numeric types.
Object
是Java中的根类,就像您的情况一样, T
不扩展任何显式类, T
是从对象
隐式.
因此,您可以在变量中使用原始类型,并可以操作对象.
我想编译器认为未指定的 T
的返回类型是 T
的最具体且兼容的类型,在您的情况下,它是 Object
.
您与集合具有相同的行为:在编译时,遇到 T
时,原始的 java.util.List
会操纵 Object
.
Object
is the root class in Java and as in your caseT
doesn't extend any explicit class, T
derives from Object
implicitly.
So, it you use a raw type in your variable, you manipulate objects.
I suppose that the compiler considers that the returned type of unspecified T
is the most specific and compatible type for T
and in your case it is Object
.
You have the same behavior with a collection : at compile-time, a raw java.util.List
manipulates Object
when T
is encountered.
修改:在这里,我将为您提供另一个示例,以说明使用原始类型而不是声明类型,如果在类中声明的类型扩展了另一个类,则编译器不一定会使用 Object
类.与您的想法相反.
Edit :
Here, I will give you another example to illustrate that with raw types, instead of declare type, the Object
class is not necessarily used by the compiler if the type declared in the class extends another class. Contrary to what you may think.
如果 Generic
类是这样声明的:
public class Generic<T extends MyClass>{
...
}
即使在变量的声明中使用原始类型, get_a()
也会返回 MyClass
对象,因为 T的最具体且兼容的类型
不是 Object
,而是 MyClass
.
Even by using a raw type in the declaration of the variable, get_a()
would return a MyClass
object since the most specific and compatible type for T
is not Object
but MyClass
.
Generic gen = new Generic(1);
MyClass myClass = gen.get_a(new MyClass());
这篇关于没有类型参数的Java通用类实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!