English 中文(简体)
Java 一般类型的论点[重复]
原标题:Java generic type arguments [duplicate]

我有一种方法,将<代码>List<?>作为论据。

public static String method(List<?> arg){
     // Do something based on type of the list
}

//I call the method as below
List<ClassA> listA = new ArrayList<ClassA>();
List<ClassB> listB = new ArrayList<ClassB>();
method(listA);
method(listB);

method中,我如何知道:argList > 。

最佳回答

From an answer by the user called Romain " If you use < ?>, you mean you aren t going to use the parametrized type anywhere. Either go to the specific type (in your case it seems List< String>) or to the very generic List< Object> "

此外,我认为,如果你使用问题标出,汇编者在运行时间(修改;有效 Java第119页)之前就赢得了捕获类型不匹配,绕过了时代,并有效地使你从使用通用类型中受益?

回答提问者的问题:如果你使用清单和提纲;反对;然后试图将其投到A或B, 可能的话使用实例 其中,这可能是了解情况的一种方法。 我要说的是,这样做的最好办法。

问题回答

Technically speaking, you CAN use instanceof to check if an object is a certain type.

但是...... 《<><><><><0>>>>>>>>>。

您公布方法的方式,可以接受任何种类的清单,因此必然会是A或B。

It s hard to tell what you re trying to do, but you probably should make your method generic.

You can do so like this:

public static <T> String method(List<T> arg) {
    // We now know that the type of the list is T, which is
    // determined based on the type of list passed to this
    // method.  This would demonstrate the point better if
    // the return type was T, but I m leaving the return type
    // as String, because that s what your code returns.
}

www.un.org/spanish/ecosoc 这里有一个较好的例子::

如果你想建立一种通用的方法,把清单的第一个要素归来,你会这样做:

public static <T> T firstElement(List<T> theList) {
    if (theList == null) {
        return null;
    }
    T objectOfTypeT = theList.get(0);
    return objectOfTypeT;
}

注:回归类型现为<代码>。 T。

由于我们使这一方法具有通用性,它可以回去清单中使用的那种方法。

通常,你只是返回<代码>theList.get(0),但我增加了一条线,使一般用途更为明显。

<>Explanation of syntax:

  • <代码><T>表示这种方法采用一种称为T的参数。

  • T,即返回类型(与通常返回Sting、Integer等)。

  • The T in the List parameter is how the compiler knows what the heck a T is.

这使得汇编者能够说:“这一方法期望的是T. Oh look...的某种类型。 清单也是T类。 如果有人通过这一方法排出“强”名单,那么T必须是“强硬”。 如果有人通过这种方法填写一份清单,T必须是一种 Integer。

相比之下,你的计算方法只能是 ,它并不了解清单中所用的类型。


Also...

如果A和B两人都延长同一类别,即“TheParentClass”,你可以宣布:

public static String method(List<? extends TheParentClass> arg)

这样,你们就更了解贵方参数的可能类型(并且可以汇编时间类型检查)。





相关问题
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 ...