国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Java泛型的編譯問題
世界只因有你
世界只因有你 2017-05-17 10:03:13
0
1
610

源代碼如下跳至下面提示符,這段代碼顯然是無法編譯的

我認為一個原因是

 BoundedEcho<String> stringEcho = new BoundedEcho<String>();

這里的String無法繼承Number,他不是String的子類?這樣理解對么?

然后另一個問題是,最后那段我傳入了一個new BoundedEcho<Integer> object, 而且他是BoundedEcho<T>的, 為什么這里會報錯呢?

是否將BoundedEcho改為public class BoundedEcho<? extends Number> {...}就對了?

源代碼在這里

public class BoundedEcho<T extends Number> {

    public T echo(T value) {
        return value;
    }

    public BoundedEcho<T> echo(BoundedEcho<T> value) {
        return value;
    }
}
public class BoundedEchoChamber{
    public static void main(String[] args) {
        BoundedEcho<Number> numberEcho = new BoundedEcho<Number>();
        numberEcho.echo(10);
        numberEcho.echo(10d);
        numberEcho.echo(10f);
        numberEcho.echo(10L);
    
        BoundedEcho<String> stringEcho = new BoundedEcho<String>();
        
        numberEcho.echo(new BoundedEcho<Integer>());
        numberEcho.echo(new BoundedEcho<Double>());
        numberEcho.echo(new BoundedEcho<Float>());
        numberEcho.echo(new BoundedEcho<Long>());
    }
}
世界只因有你
世界只因有你

全部回復(fù)(1)
過去多啦不再A夢

問題出在這兩句

public BoundedEcho<T> echo(BoundedEcho<T> value) {
        return value;
    }
    
BoundedEcho<Number> numberEcho = new BoundedEcho<Number>();

實例化的時候你把T聲明成了Number,之后調(diào)用就必須是BoundedEcho<Number>。原因是BoundedEcho<Integer>等類型和BoundedEcho<Number>是不同的類,并不存在繼承關(guān)系。

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板