- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我们有如下两种方法:
public static <T> T genericReturn() { /*...*/ }
public static String stringReturn() { /*...*/ }
在调用任何方法时,无论是否有任何要求,您都可以提供类型见证:
String s;
s = Internet.<String>genericReturn(); //Type witness used in return type, returns String
s = Internet.<Integer>stringReturn(); //Type witness ignored, returns String
但是,我在 Java 中根本没有看到任何实际用途,除非无法推断类型(这通常表明存在更大的问题)。此外,如果使用不当,它就会被忽略,这一事实似乎违反直觉。那么在 Java 中使用这个有什么意义呢?
最佳答案
来自JLS §15.2.12.1 :
- If the method invocation includes explicit type arguments, and the member is a generic method, then the number of type arguments is equal to the number of type parameters of the method.
This clause implies that a non-generic method may be potentially applicable to an invocation that supplies explicit type arguments. Indeed, it may turn out to be applicable. In such a case, the type arguments will simply be ignored.
后面是理由
This rule stems from issues of compatibility and principles of substitutability. Since interfaces or superclasses may be generified independently of their subtypes, we may override a generic method with a non-generic one. However, the overriding (non-generic) method must be applicable to calls to the generic method, including calls that explicitly pass type arguments. Otherwise the subtype would not be substitutable for its generified supertype.
沿着这个推理思路,让我们构建一个示例。假设在Java 1.4中,JDK有一个类
public class Foo
{
/** check obj, and return it */
public Object check(Object obj){ ... }
}
一些用户编写了一个扩展 Foo
的专有类并覆盖 check
方法
public class MyFoo extends Foo
{
public Object check(Object obj){ ... }
}
当 Java 1.5 引入泛型时,Foo.check
概括为
public <T> T check(T obj)
雄心勃勃的向后可比性目标要求 MyFoo
仍然可以在 Java 1.5 中编译,无需修改;和MyFoo.check[Object->Object]
仍然是 Foo.check[T->T]
的重写方法。
现在,根据上述理由,由于可以编译:
MyFoo myFoo = new MyFoo();
((Foo)myFoo).<String>check("");
这也必须编译:
myFoo.<String>check("");
尽管MyFoo.check
不是通用的。
这听起来有些牵强。但即使我们接受这个论点,解决方案仍然过于宽泛和过度。 JLS 可以收紧它,以便 myFoo.<String,String>check
和obj.<Blah>toString()
是非法的,因为类型参数数量不匹配。他们可能没有时间解决这个问题,所以他们只是走了一条简单的路线。
关于java - 在所有方法调用上允许类型见证有什么意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30811128/
我有一张 table People (First_Name, Last_Name)。此表包含与示例中一样重复的记录(并非所有行都重复): First_Name Last_Name John
我用 Java 编写过很多程序,之前也涉足过 C++。我在各种 C++ 书籍中阅读了有关指针的内容,并完成了书籍中的各种示例。 我了解指针的基础知识,但有一件事我一直不清楚。指针在现实世界中的应用是什
线 .Mappings(m => m.FluentMappings.AddFromAssemblyOf() 它有什么作用?它会在派生自 ClassMap 的 Product 类的程序集中查找任
我有用于打印数字的自定义打印功能。我制作了一个 ASCII 版本和一个 UTF-16LE 版本。 UTF-16LE 版本对 0-9 使用全角代码/字符,对十六进制使用 A-F。在调试我的函数时,我注意
这是我的代码片段: float ab(float); 以后 if(ab(temp)
我在一个项目文件中包含以下代码: //begin of the file ((window) => { 'use strict'; class View extends GSM.Event
我一直在到处寻找关于 ? 用法的正确解释。和 *。我注意到我可以使用以下方法拒绝所有用户的访问: 如果我想允许某个组,我应该在其上方添加下一行: 但是当我看到人们使用 ? 时,我开始忘记什么意思,
我正在关注 melon js tutorial .这是在我的 HUD.js 文件的顶部。 game.HUD = game.HUD || {} 我以前在其他例子中见过这个。 namespace.some
我正在处理一个包含数千行代码的文件。我正在第 700 行实现一个算法。我经常不得不离开这些行来检查文件中的其他方法。 导航回到我实际编码的地方通常很痛苦。如果我可以在第 700 行设置一个航路点并为其
我遇到了这段代码 do { if (higherQuality && w > targetWidth) { w /= 2; if (w &
uint8_t * const LCDMem = (uint8_t *) &LCDM3; 此代码在 msp430fg4618 培训套件中用于 lcd 配置。谁能解释一下上述代码的含义? 它允许使用 a
上下文 阅读一些内核代码。 问题 我不明白这行是什么意思 *(void **) &(int[2]){0,PAGE_SIZE}; 还有更多,这是什么意思 {0,PAGE_SIZE} 对我来说,它看起来不
我正在查看 Underscore.js 的源代码库,专门用于 map方法(该页面第 85 行左右,并复制到此处): _.map = function(obj, iterator, context)
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我是一名优秀的程序员,十分优秀!