我正在寻找GWT的一些GXT代码,我遇到了泛型的这种使用,我在Java教程中找不到另一个例子.com.extjs.gxt.ui.client.data.BaseModelData如果要查看所有代码,则为类名.以下是重要部分:
private RpcMap map;
public <X> X get(String property) {
if (allowNestedValues && NestedModelUtil.isNestedProperty(property)) {
return (X)NestedModelUtil.getNestedValue(this, property);
}
return map == null ? null : (X) map.get(property);
}
Run Code Online (Sandbox Code Playgroud)
X在类中或层次结构中的任何地方都没有定义,当我在eclipse中点击"go to declaration"时,它只会进入<X>公共方法签名.
我试着用以下两个例子调用这个方法来看看会发生什么:
public Date getExpiredate() {
return get("expiredate");
}
public String getSubject() {
return get("subject");
}
Run Code Online (Sandbox Code Playgroud)
他们编译并显示没有错误或警告.我认为至少我必须做一个演员才能让它发挥作用.
这是否意味着Generics允许魔法返回值可以是任何东西,并且会在运行时爆炸?这似乎与泛型应该做的事情相反.任何人都可以向我解释这一点,并可能给我一些链接到一些文档,解释这一点好一点?我已经浏览了Sun关于泛型的23页pdf,并且返回值的每个示例都是在类级别定义的,或者是传入的参数之一.
我一直在尝试这样的方法,但找不到任何解决方案:
public static JSONObject or JSONArray objectToJSON(Object object){
if(object is a JSONObject)
return new JSONObject(object)
if(object is a JSONArray)
return new JSONArray(object)
}
Run Code Online (Sandbox Code Playgroud)
我试过这个:
public static JSONObject objectToJSONObject(Object object){
Object json = null;
try {
json = new JSONTokener(object.toString()).nextValue();
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject jsonObject = (JSONObject)json;
return jsonObject;
}
public static JSONArray objectToJSONArray(Object object){
Object json = null;
try {
json = new JSONTokener(object.toString()).nextValue();
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray jsonObject = (JSONArray)json;
return …Run Code Online (Sandbox Code Playgroud) 我是Java的新手,所以我不确定这是否可行.基本上我需要将文件反序列化为给定类型的对象.基本上该方法将执行此操作:
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fis);
MyClass newObject = (MyClass)in.readObject();
in.close();
return newObject;
Run Code Online (Sandbox Code Playgroud)
我希望这个方法是通用的,因此我可以告诉它我想要in.readObject()将其输出转换为什么类型,并返回它.
希望这是有道理的...然后再说一遍,我可能没有正确理解泛型,这实际上是不可能的,或者是可取的.
感谢:D.
是否可以根据参数更改Java中的方法签名?
例:
给定一个类,带有泛型参数MyItem<T>.假设这个类有一个方法,它返回T
给定第二个类'myReturner()',它包含方法myreturn(MyItem<T>).
题:
我可以myreturn(MyItem<T>)返回一个T对象,具体取决于MyItem的泛型参数吗?
我想这是不可能的,因为签名是在Java编译期间设置的,并且在编译时不知道T. 如果是这样,模拟方法的最佳方法是什么,它将返回不同的对象,具体取决于参数?是为每种参数类型编写自己的方法唯一的方法吗?
我遵循这段代码:
public <T extends ParentException> T managedException(Exception cause) {
if(ExceptionA.class.isInstance(cause)) {
return ExceptionA.class.cast(cause);
} else if(ExceptionB.class.isInstance(cause)) {
return ExceptionB.class.cast(cause);
} else if(ExceptionC.class.isInstance(cause)){
return ExceptionC.class.cast(cause);
} else {
return new ExceptionD(cause.getMessage(), cause);
}
}
Run Code Online (Sandbox Code Playgroud)
在这里ExceptionA,ExceptionB,ExceptionC,ExceptionD是儿童ParentException.
在编译时,我得到了错误:
incompatible types: ExceptionA cannot be converted to T
incompatible types: ExceptionB cannot be converted to T
incompatible types: ExceptionC cannot be converted to T
incompatible types: ExceptionD cannot be converted to T
Run Code Online (Sandbox Code Playgroud)
但是,如果我将代码更改为:
@SuppressWarnings("unchecked")
public …Run Code Online (Sandbox Code Playgroud) 如何编写一个返回动态类型的方法(如果可以的话)
就像是
public X createRequestObject(Class xclass , String url , String username , String password){
X x = Class.forName(xclass.getCannonicalName()).getConstructor(String.class).newInstance(url);
x.setheader("AUTHORIZATION" , createHeader(username,password)
return x
}
Run Code Online (Sandbox Code Playgroud)
然后我可以像使用它一样
HttpGet httpGet = createRequestObject(HttpGet.class , "http://wwww.google.com , "username","password");
or
HttpPost httpPost = createRequestObject(HttpPost.class , "http://wwww.google.com , "username","password");
Run Code Online (Sandbox Code Playgroud)
我知道我可以返回一个对象,然后再投出它,但我不喜欢演员,所以想知道java中是否有一个可以帮助我做这个的构造
我一直在为学校做项目.基本上你必须用几个扫描仪制作一个脚本.例如:
我现在想为这两个函数制作一个Scanner类.但是1必须是一个整数,第二个必须是一个整数.如何确保函数返回double或整数.
我使用以下代码:
public static [Heres what goes wrong] vrager (String type, String tekst) {
Scanner vraag = new Scanner(System.in);
System.out.println(tekst);
type variable = vraag.next();
return variable;
Run Code Online (Sandbox Code Playgroud)
所以通过调用函数就像: seconden = (vrager("int", "How many seconds?:));
但是,如果我想让函数也能工作,那么它就会出错,因为函数不会期望返回双精度.
我该如何解决这个问题?
当我尝试编译以下代码时,编译失败并出现以下错误.我不知道为什么它应该,因为我只返回一个实现合同的类
public interface Contract {
static <T extends Contract> T get() {
return new ConcreteContract();
}
}
class ConcreteContract implements Contract {
}
Run Code Online (Sandbox Code Playgroud)
Contract.java:3: error: incompatible types: ConcreteContract cannot be converted to T
return new ConcreteContract();
^
where T is a type-variable:
T extends Contract declared in method <T>get()
1 error
Run Code Online (Sandbox Code Playgroud)
有没有人知道为什么java表现这种方式(或)我错过了一些明显的东西
PS:在发布此查询之前,我已在SO中阅读了超过10个热门搜索