sky*_*ren 5 java compiler-construction javac java-7
在Java 6中,我能够使用:
public static <T, UK extends T, US extends T> T getCountrySpecificComponent(UK uk, US us) {
Country country = CountryContext.getCountry();
if (country == Country.US) {
return us;
} else if (country == Country.UK) {
return uk;
} else {
throw new IllegalStateException("Unhandled country returned: "+country);
}
}
Run Code Online (Sandbox Code Playgroud)
使用这些存储库:
public interface Repository{
List<User> findAll();
}
public interface RepositoryUS extends Repository{}
public interface RepositoryUK extends Repository{}
Run Code Online (Sandbox Code Playgroud)
使用这些时:
RepositoryUK uk = ...
RepositoryUS us = ...
Run Code Online (Sandbox Code Playgroud)
这行在Java6中编译但在Java7中失败(错误找不到符号 - 因为编译器在类Object上查找findAll())
List<User> users = getCountrySpecificComponent(uk, us).findAll();
Run Code Online (Sandbox Code Playgroud)
这在Java 7中编译
List<User> users = ((Repository)getCountrySpecificComponent(uk, us)).findAll();
Run Code Online (Sandbox Code Playgroud)
我知道这是一个相当罕见的用例,但有没有理由进行这种改变?还是一种告诉编译器有点"更聪明"的方法?
| 归档时间: |
|
| 查看次数: |
246 次 |
| 最近记录: |