我需要此代码来接受所有数字:站应该能够以整数、双精度或浮点数的形式输入。我的问题是专门关于接口的。我在这里正确使用泛型吗?我应该用“T”替换所有“Double”实例吗?
干杯和感谢。
public class generics {
public class WrongStation extends Exception {
public WrongStation () { super(); }
public WrongStation (String message) { super(message); }
public WrongStation (String message, Throwable cause) { super(message, cause); }
public WrongStation (Throwable cause) { super(cause); }
}
interface Changeable <T> extends Collection <T> {
void setChannel(Number channelNumber) throws wrongStation;
Number getChannel();
Number getMinChannel();
Number getMaxChannel();
Number getChannelIncrement();
}
就使用泛型而言,上面的代码看起来是否正确?无论用户输入整数、双精度数还是浮点数,我都希望程序运行。这是程序的其余部分:
class FMRadio implements Changeable {
double channel = 88.1;
public void setChannel(Number newChannel) {
System.out.println("HAM Radio channel set to: " + newChannel);
channel (Double) = (Double)newChannel;
}
public T getChannel() {
return channel;
}
public Double getMinChannel() {
return 88.1;
}
public Double getMaxChannel() {
return 107.9;
}
public Double getChannelIncrement() {
return 0.2;
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean contains(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public Iterator iterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object[] toArray(Object[] a) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean add(Object e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean remove(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean containsAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean retainAll(Collection c) {
// TODO Auto-generated method stub
return false;
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
}
public static class HamRadio {
public void main(String[] args) {
HamRadio myRadio = new HamRadio();
myRadio.setChannel(133.9);
myRadio.setChannel(300);
}
}}