简体   繁体   English

Java中的参数类

[英]Parametric class in Java

I'm a little bit confused about this code, it is a test to understand the parametric class. 我对此代码有些困惑,这是了解参数类的测试。

I have 2 classes, one is the Main class and the other is the parametric class. 我有2个类,一个是Main类,另一个是参数类。 When I create a new Object with Integer as a parameter and I think into the constructor will call the print(Integer x) method, but it is not like this, instead java will call the print(Object o) method. 当我使用Integer作为参数创建一个新的Object时,我认为进入构造函数将调用print(Integer x)方法,但事实并非如此,而是java将调用print(Object o)方法。

Someone of you know why it happened? 你们当中有人知道为什么会这样吗?

package classi.parametriche;

public class Contenitore <E> {
public E variabile;

public Contenitore(E value){
    variabile = value;
    System.out.println("Variabilie : "+variabile.getClass().toString());
    System.out.println("Variabile : "+value.getClass().toString());
    println(variabile);
}
public void println(String s){
    System.out.println("Stringa : "+ s);
}
public void println(Integer x){
    System.out.println("Int : " + x);
}
public void println(short x){
    System.out.println("short : " + x);
}
public void println(byte x){
    System.out.println("byte : " + x);
}
public void println(long x){
    System.out.println("long : " + x);
}
public void println(char x){
    System.out.println("char : " + x);
}
public void println(float x){
    System.out.println("float : " + x);
}
public void println(double x){
    System.out.println("double : " + x);
}
public void println(Object o){
    if (o != null){
        System.out.println("Object : " + o.toString());
    }else{
        System.out.println("null");
    }
}

} }

public class ClassiParametriche {
    public static void main(String[] args) {
        Contenitore<Integer> c = new Contenitore <Integer>(42);

    }
}

this is the result : 这是结果:

 run:
 Variabilie : class java.lang.Integer
 Variabile : class java.lang.Integer
 Object : 42

This is because of type erasure and compile time binding. 这是因为类型擦除和编译时绑定。

The compiler decides at compile time which of the println methods is called. 编译器在编译时决定调用哪个println方法。 Because the field of your class is a generic with no defined super class the remaining type after type erasure is Object. 由于您的类的字段是通用的,没有定义的超类,因此类型擦除后的剩余类型为Object。 This means the field variable is internally handled as an Object. 这意味着字段变量在内部作为对象处理。

You can also check this post for details. 您也可以查看帖子以获取详细信息。

The answer is type-erasure. 答案是消除类型。 At run-time the class has no parameter type. 在运行时,该类没有参数类型。 Generics in Java is purely compile-time sugar. Java中的泛型纯粹是编译时的糖。 Once the code is compiled, it loses any knowledge of the associated parameter type. 一旦代码被编译,它将失去对相关参数类型的任何了解。 So when you pass the Integer into the constructor, the constructor is actually expecting a variable of type java.lang.Object 因此,当您将Integer传递给构造函数时,构造函数实际上期望的是类型为java.lang.Object的变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM