我有一个简单的foo类,我能够转换到集合接口(Map或者List),没有任何编译器错误.请注意,Foo类不实现任何接口或扩展任何其他类.
public class Foo {
public List<String> getCollectionCast() {
return (List<String>) this; // No compiler error
}
public Map<String, String> getCollection2Cast() {
return (Map<String, String>) this; // No compiler error
}
public Other getCast() {
return (Other)this; // Incompatible types. Cannot cast Foo to Other
}
public static class Other {
// Just for casting demo
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试将类转换为集合时,为什么Java编译器不会返回不兼容的类型错误Foo?
Foo没有实现Collection.我期望一个不兼容的类型错误,因为给定当前Foo类签名,这不能是一个Collection.
在MediaPlayer.create方法中,可以使用到Raw文件的id但是如何在setDataSource方法中使用它?