简体   繁体   English

理解类<?>

[英]Understanding Class<?>

I came across this code: 我遇到了这段代码:

public class RestfulAdage extends Application {
  @Override
  public Set<Class<?>> getClasses() {
    Set<Class<?>> set = new HashSet<Class<?>>();
    set.add(Adages.class);
    return set;
  }
}

I do not understand what Class<?> means. 我不明白Class<?>意思。

Class<?> refers to a class of unknown type. Class<?>指的是一类未知类型。 The notation uses an unbounded generic which places no restriction on the type of class that can be added to the Collection. 该表示法使用无界泛型 ,它对可添加到Collection的类的类型没有限制。 For example the following would not work 例如,以下内容不起作用

Set<Class<String>> set = new HashSet<Class<String>>();
set.add(Adages.class); // type not allowed

Class is a parametrizable class, hence you can use the syntax Class where T is a type. Class是一个可参数化的类,因此您可以使用T是类型的语法Class。 By writing Class, you're declaring a Class object which can be of any type (? is a wildcard). 通过编写Class,您将声明一个可以是任何类型的Class对象(?是通配符)。 The Class type is a type that contains metainformation about a class. 类类型是包含有关类的元信息的类型。

It's always good practice to refer to a generic type by specifying his specific type, by using Class you're respecting this practice (you're aware of Class to be parametrizable) but you're not restricting your parameter to have a specific type. 通过指定他的特定类型来引用泛型类型总是很好的做法,通过使用Class你尊重这种做法(你知道Class可以参数化),但是你并没有限制你的参数具有特定的类型。

Reference about Generics and Wildcards: http://docs.oracle.com/javase/tutorial/java/generics/wildcards.html 关于泛型和通配符的参考: http//docs.oracle.com/javase/tutorial/java/generics/wildcards.html

Reference about Class object and reflection the (feature of Java language used to introspect itself): http://java.sun.com/developer/technicalArticles/ALT/Reflection/ 关于Class对象和反射的引用(用于内省自身的Java语言的特性): http//java.sun.com/developer/technicalArticles/ALT/Reflection/

In generic code, the question mark (?), called the wildcard, represents an unknown type. 在通用代码中,称为通配符的问号(?)表示未知类型。 The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; 通配符可用于各种情况:作为参数,字段或局部变量的类型; sometimes as a return type (though it is better programming practice to be more specific). 有时作为返回类型(虽然更好的编程实践更具体)。 The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype. 通配符从不用作泛型方法调用,泛型类实例创建或超类型的类型参数。

The following sections discuss wildcards in more detail, including upper bounded wildcards, lower bounded wildcards, and wildcard capture. 以下部分更详细地讨论通配符,包括上限有界通配符,下限有界通配符和通配符捕获。

for more information click here 欲了解更多信息, 请点击此

It refers to gererics. 它指的是gererics。 I suggest you read a little on it. 我建议你读一点。 Basically, you know only at runtime what type of object you get to work with. 基本上,您只知道在运行时使用哪种类型的对象。 For example, Class can be Integer, String or even YourDefinedClassType 例如,Class可以是Integer,String甚至是YourDefinedClassType
read here http://java.sun.com/developer/technicalArticles/J2SE/generics/ 在这里阅读http://java.sun.com/developer/technicalArticles/J2SE/generics/

From : Wildcards 来自: 通配符

In generic code, the question mark (?), called the wildcard, represents an unknown type. 在通用代码中,称为通配符的问号(?)表示未知类型。 The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; 通配符可用于各种情况:作为参数,字段或局部变量的类型; sometimes as a return type (though it is better programming practice to be more specific). 有时作为返回类型(虽然更好的编程实践更具体)。 The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype. 通配符从不用作泛型方法调用,泛型类实例创建或超类型的类型参数。

Check the link, you will find more exhaustive documentation, examples etc. 查看链接,您将找到更详尽的文档,示例等。

Assume that you have a set of classes that belong to different types , and you have instances of different classes as well. 假定你有一组属于不同类型的 ,并且你有不同的的实例也是如此。 So if you want to check whether these instances are instanceof one of these classes, you could iterate through these set and do the job. 因此,如果要检查这些实例是否是这些类之一的实例 ,您可以遍历这些集并完成工作。 And for that kind of job, you better use a totally unrestricted set: 对于那种工作,你最好使用完全不受限制的套装:

public boolean checkClasses(Set<Class<?>> typeLessClassSet, Set instances){

    while(typeLessClassSet.hasNext()){
        Class c = typeLessClassSet.next();
        while(instances.hasNext()){
            Object o = instances.next();
            if(o instanceof c)
            return true;
        }
    }
        return false;    
}

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

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