源代码如下跳至下面提示符,这段代码显然是无法编译的
我认为一个原因是
BoundedEcho stringEcho = new BoundedEcho();
这里的String无法继承Number,他不是String的子类?这样理解对么?
然后另一个问题是,最后那段我传入了一个new BoundedEcho object, 而且他是BoundedEcho
是否将BoundedEcho改为public class BoundedEcho extends Number> {...}就对了?
源代码在这里
public class BoundedEcho {
public T echo(T value) {
return value;
}
public BoundedEcho echo(BoundedEcho value) {
return value;
}
}
public class BoundedEchoChamber{
public static void main(String[] args) {
BoundedEcho numberEcho = new BoundedEcho();
numberEcho.echo(10);
numberEcho.echo(10d);
numberEcho.echo(10f);
numberEcho.echo(10L);
BoundedEcho stringEcho = new BoundedEcho();
numberEcho.echo(new BoundedEcho());
numberEcho.echo(new BoundedEcho());
numberEcho.echo(new BoundedEcho());
numberEcho.echo(new BoundedEcho());
}
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
问题出在这两句
实例化的时候你把T声明成了Number,之后调用就必须是BoundedEcho<Number>。原因是BoundedEcho<Integer>等类型和BoundedEcho<Number>是不同的类,并不存在继承关系。