English 中文(简体)
B. 野生心脏衰减类型混淆
原标题:confusion regarding wild card parametrized types

我有2个班级如下。 根据我的了解,编辑不能在第4行(HandCard)上提出申诉,因为我的分化类型是(Node?) 超级编号和编号;0),因为物体在不测量的类型中是多余的。 而汇编者则在4、8和9处提出申诉。 因此。

public class Node<E> {

    private E data;

    public void setData(E obj) {
        data = obj;
    }

    public E getData() {
        return data;
    }
}


public class WildCard {


    static void checkIt(Node<? super Number> s0)
    {
        Object object=new Object(); //1
        Number number =1.5; //2
        Integer integer=10; //3

        s0.setData(object); //4
        s0.setData(number); //5
        s0.setData(integer); //6

        object=s0.getData(); //7
        number=s0.getData(); //8
        integer=s0.getData(); //9
    }
}
最佳回答

问题如下:

Node<? 超级编号和编号;系指在任何<代码>Node上通过,其参数为Number或超级类型,例如Object

Since you don t know the exact type of that generic parameter some operations are not allowed.

下面是一些行文汇编的原因,有些则没有:

  • s0.setData(object); doesn t compile because you could have a Node<Number> which would not all you to add an arbitrary object
  • s0.setData(number); compiles since Number matches all possible types for the parameter
  • s0.setData(integer); compiles because Integer extends Number and thus the line above applies

  • object=s0.getData(>);汇编,因为任何数据物体均填入<代码>。

  • number=s0.getData(); doesn t compile because you could have a Node<Object> and thus the data object might have a different type (e.g. String)
  • integer=s0.getData(); doesn t compile, same reason as the line above
问题回答

The Java Language Specification says:

Wildcards may be given explicit bounds, just like regular type variable declarations. An upper bound is signified by the following syntax, where B is the bound:

? extends B

Unlike ordinary type variables declared in a method signature, no type inference is required when using a wildcard. Consequently, it is permissible to declare lower bounds on a wildcard, using the following syntax, where B is a lower bound:

<代码> 超级B

Reference(T referent, ReferenceQueue<? super T> queue);

Here, the referent can be inserted into any queue whose element type is a supertype of the type T of the referent; T is the lower bound for the wildcard.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签