Java 泛型:泛型类型仅定义为返回类型 [英] Java Generics: Generic type defined as return type only

查看:50
本文介绍了Java 泛型:泛型类型仅定义为返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 GWT 的一些 GXT 代码,我遇到了在 Java 教程中找不到其他示例的泛型的这种用法.类名是 com.extjs.gxt.ui.client.data.BaseModelData 如果您想查看所有代码.以下是重要部分:

I'm looking at some GXT code for GWT and I ran across this use of Generics that I can't find another example of in the Java tutorials. The class name is com.extjs.gxt.ui.client.data.BaseModelData if you want to look at all of the code. Here are the important parts:

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);
}

X 在类或层次结构中的任何其他地方都没有定义,当我在 Eclipse 中点击转到声明"时,它只会转到 <X> 在公共方法签名中.

X is defined nowhere else in the class or anywhere in the hierarchy, and when I hit "go to declaration" in eclipse it just goes to the <X> in the public method signature.

我尝试使用以下两个示例调用此方法以查看会发生什么:

I've tried to call this method with the following two examples to see what happens:

public Date getExpiredate() {
    return  get("expiredate");
}

public String getSubject() {
    return  get("subject");
}

它们编译并没有显示错误或警告.我认为至少我必须做一个演员才能让它发挥作用.

They compile and show no errors or warnings. I would think at the very least I would have to do a cast to get this to work.

这是否意味着泛型允许一个神奇的返回值,它可以是任何东西并且只会在运行时爆炸?这似乎与泛型应该做的事情背道而驰.任何人都可以向我解释这一点,并可能给我一个链接到一些更好地解释这一点的文档?我已经阅读了 Sun 关于泛型的 23 页 pdf,并且返回值的每个示例都是在类级别或在传入的参数之一中定义的.

Does this mean that Generics allow a magic return value that can be anything and will just blow up at runtime? This seems counter to what generics are supposed to do. Can anyone explain this to me and possibly give me a link to some documentation that explains this a little better? I've went through Sun's 23 page pdf on generics and every example of a return value is defined either at the class level or is in one of the parameters passed in.

推荐答案

该方法返回您期望的任何类型( 在方法中定义并且绝对无界).

The method returns a type of whatever you expect it to be (<X> is defined in the method and is absolutely unbounded).

这是非常非常危险的,因为没有规定返回类型实际上与返回值匹配.

This is very, very dangerous as no provision is made that the return type actually matches the returned value.

这样做的唯一优点是您不必强制转换此类可以返回任何类型的通用查找方法的返回值.

The only advantage this has is that you don't have to cast the return value of such generic lookup methods that can return any type.

我想说:小心使用这样的结构,因为你几乎失去了所有的类型安全性,而只是你不必在每次调用 get().

I'd say: use such constructs with care, because you lose pretty much all type-safety and gain only that you don't have to write an explicit cast at each call to get().

是的:这几乎是黑魔法,它在运行时爆炸并破坏了泛型应该实现的整个想法.

And yes: this pretty much is black magic that blows up at runtime and breaks the entire idea of what generics should achieve.

这篇关于Java 泛型:泛型类型仅定义为返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆