java – 如何从一般的特定类获取一个类文字

前端之家收集整理的这篇文章主要介绍了java – 如何从一般的特定类获取一个类文字前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_0@
有这样的方法需要类文字作为参数.
Collection<EmpInfo> emps =  
  SomesqlUtil.select(  
  EmpInfo.class,"select * from emps");

要么

GWT.create(Razmataz.class);

当我需要提供通用的特定类时,问题就出现了

EmpInfo<String>
Razmataz<Integer>

以下将是错误的语法

Collection<EmpInfo<String>> emps =  
  SomesqlUtil.select(  
  EmpInfo<String>.class,"select * from emps");

要么

GWT.create(Razmataz<Integer>.class);

因为你不能做类似的语法

Razmataz<Integer>.class

那么,我怎么能挤出一个班级文字

EmpInfo<String>
Razmataz<Integer>

所以我可以把它们作为需要类文字方法的参数?

更多信息

好的,我承认我主要是要求GWT.

我有一对GWT RPC接口Razmataz. (FYI,GWT RPC接口必须在服务器 – 客户端对中定义).我打算使用相同的接口对来进行通信,无论是String,Integer,Boolean等.

Razmataz< T>的GWT.create(Razmataz)
抱怨说,由于我没有指定T,GWT编译器将其视为对象.那么GWT编译器不会接受Object类.它需要比作为一个对象更具体.

所以,似乎没有办法告诉GWT.创建T是什么,因为一个类文字是一个运行时概念,而泛型是一个编译时的概念,对吗?

解决方法

报价从 Java Generics and Collections,第7.2节:

Class literals are also restricted; it is not even syntactically valid to supply a type parameter to the type in a class literal. Thus,the following fragment is illegal:

class ClassLiteral {
  public Class<?> k = List<Integer>.class;  // Syntax error
}

Indeed,Java’s grammar makes a phrase such as the preceding one difficult to parse,and it may trigger a cascade of Syntax errors […]

This Syntax problem leads to an irregularity. Everywhere else that a reifiable type is required,you may supply either a raw type (such as List) or a parameterized type with unbounded wildcards (such as List<?>). However,for class tokens,you must supply a raw type; not even unbounded wildcards may appear. Replacing List<Integer> with List<?> in the preceding code leads to a similar error cascade.

所以,别无选择,只能在类标记中使用原始类型

GWT.create(Razmataz.class);
原文链接:https://www.f2er.com/java/122184.html

猜你在找的Java相关文章