- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
有什么方法可以将封闭类声明的类型变量用作内部类中声明的类型变量的绑定(bind)?
class Test<E> {
class Inner<T extends E> {}
<T extends E> void doStuff(T arg) {}
public static void main(String[] args) {
new Test<Number>().doStuff(new Integer(0)); // works fine, as expected
new Test<Number>().new Inner<Integer>(); // won't compile
}
}
javac 给出了这个错误:
Test.java:6: type parameter java.lang.Integer is not within its bound
new Test<Number>().new Inner<Integer>();
^
我找不到能满足编译器要求的任何类型组合。 Inner
声明的类型参数 T
与 doStuff
之间有什么区别?为什么一个有效而另一个无效?
我不是在寻找替代方案,我只是想更好地了解该语言的工作原理。
最佳答案
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6557954
Bug ID: 6557954
Votes 2
Synopsis Inner class type parameters doesn't get substituted when checking type well-formedness
Category java:compiler
Release Fixed 7(b40)
State 10-Fix Delivered, bug
Priority: 5-Very Low
Submit Date 16-MAY-2007
Posted Date : 2008-07-02 16:22:46.0
说明
The compiler fails to accept this program:
class Foo<T> {
class Bar<U extends T> {}
Foo<Number>.Bar<Integer> f;
}
评价
This is a problem in Check.java as when checking for bound-conformance actual type parameters are subsituted only in topmost type-variable's bound. In this case we have that Foo.Bar is to be checked against the actual type-parameters T=Number, U=Integer
So it should be the case that:
Number <: Object
Integer <: [Number/T]T = Number
unfortunately, javac misses the second substitution so that the check becomes:
Integer <: T
which is wrong and cause the error.
编辑:
在我的系统上,问题中的代码使用 Java 7 javac
编译没有错误:
C:\workspace\Sandbox\src>"%JAVA_HOME%\bin\javac.exe" -version
javac 1.7.0-ea
但是它失败了,出现了 Java 6 javac
问题中指出的错误:
C:\workspace\Sandbox\src>"%JAVA_HOME%\bin\javac.exe" -version
javac 1.6.0_17
关于java - 通过封闭类类型变量绑定(bind)的内部类类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4823278/
从the documentation, 13.5.5: When the last parameter of a method is a closure, you can place the clos
Bjarne Stroustrup 写道: “友元类必须事先在封闭范围内声明或在非类范围内定义,立即封闭声明它为友元的类” 语句的第一部分不是多余的,因为“立即包含类的非类范围”包括“先前在封闭范围中
我有一个网格(如下例),其中包含外墙(标记为 W)、环境 block (E)、开放空间 (o) 和事件点 (A)。目前,此网格存储在 [,] 中,其中包含与给定点关联的所有数据。我试图确定是否包含一个
我正在尝试使用 this blogpost's approach to higher-kinded data without dangling Identity functors for the tr
在下面的代码中: package main import ( "fmt" "time" ) func asChan(vs ...int) <-chan int { c := m
我在传递和评估闭包列表时遇到困难。经过大量简化,该程序显示出与我正在尝试编写的程序相同的错误: use std::vec::flat_map; #[main] fn main() { let li
我正在努力成为一名好公民,并尽可能远离全局范围。有没有办法访问不在全局范围内的 setTimeout 变量? 因此,在此示例中,某人将如何取消“计时器”? myObject.timedAction =
考虑这个例子: def A(): b = 1 def B(): # I can access 'b' from here. print(b)
val listPlans: List = newPlans.mapTry { it.data.map { Plan(it.id, it.nam
我目前正在尝试使用SinonJS对我的 angular.service 进行单元测试,但是遇到了一个问题,希望有人可以阐明为什么会发生这种情况。我已经重构了当前的项目以说明当前的问题。 我还提供了DE
我正在使用 Go channel ,我想知道关闭 channel 和将其设置为 nil 之间有什么区别? 编辑: 在此example ,我想通过关闭 channel 或设置为零来断开发送者和接收者的连
我的应用程序有一个奇怪的行为,我不知道它来自哪里。我已经为 TextView 内容实现了 NSScanner,效果非常好。扫描器与文本存储结合使用,通过 TextView 委托(delegate)方法
我不知道如何让 MyBatis 生成封闭的 or 语句: WHERE x.token = ? AND ( (x.scene = 'A' OR x.scene = 'B')) 这是一个令人惊讶的简单
我不希望这是一个摄像头检测程序。这是一个程序,可以检测应用程序屏幕上颜色的传递。 我想要做的是检测大于 5x5 像素的黑色何时穿过屏幕上定义的空间区域。我想过用一个大区域来拉伸(stretch)整个宽
我一直在使用 RDFLib 来解析数据并将其插入到三元组中。我遇到的一个常见问题是,从关联数据存储库解析时,没有尖括号括起 URL。 要上传数据,我必须手动添加 并使用 URIRef重新创建 URL。
我已经阅读了很多有关此问题的帖子,但我仍然不确定我是否完全理解这些定义。 以下是我认为不同术语的示例。我是否走在正确的轨道上,或者我仍然不理解这些概念。谢谢 Array - unbound and o
我为我的 Android 应用设置了 GooglePlay 内部和封闭式 Alpha 测试设置。 它非常适合允许测试人员加入计划并安装应用程序,但是当我从测试人员电子邮件列表中删除测试人员时,他们仍然
我是一名优秀的程序员,十分优秀!