-2

我需要此代码来接受所有数字:站应该能够以整数、双精度或浮点数的形式输入。我的问题是专门关于接口的。我在这里正确使用泛型吗?我应该用“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);


            }
        }}
4

2 回答 2

2

这应该足以接受任何类型的数字:

  interface Changeable {
            void setChannel(Number channelNumber) throws wrongStation;
            Number  getChannel();
            Number getMinChannel();
            Number getMaxChannel();
            Number getChannelIncrement();
        }

用法

 class Radio implement Changeble{

或使用泛型和更精确

interface Changeable <T extends Number> {
            void setChannel(T channelNumber) throws wrongStation;
            T getChannel();
            T getMinChannel();
            T getMaxChannel();
            T getChannelIncrement();
        }

用法

class Radio implement Changeble{

或者

 class Radio implement Changeble<Double>{

或者

class Radio implement Changeble<Integer>{
于 2013-09-20T20:09:49.423 回答
0

您的接口声明Changeable看起来正确。但是,在使用FMRadio. 你会想要类似的东西

class FMRadio implements Changeable<Double> {

在这里,Double可能是您打算在您的Collection. 或者你可以使FMRadio自己通用:

class FMRadio<E> implements Changeable<E> {

无论哪种方式,您都需要更改方法签名以使用Collection接口中需要的泛型类型参数:addaddAll.

于 2013-09-20T20:07:27.903 回答