小编ist*_*tis的帖子

为什么Java允许你转换为集合?

我有一个简单的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.

java collections casting interface

65
推荐指数
2
解决办法
6183
查看次数

在MediaPlayer中将数据源设置为原始ID

在MediaPlayer.create方法中,可以使用到Raw文件的id但是如何在setDataSource方法中使用它?

android android-mediaplayer

30
推荐指数
3
解决办法
3万
查看次数