Java varargs需要无限数量的变量

时间:2016-02-06 19:29:09

标签: java variadic-functions

我正在尝试应用varargs。我已经声明了一个方法,它需要无限量的变量,如:

private Subject carMonitor;

public AdvancedMonitor(Subject ... carMonitors){
    for (Subject carMonitor : carMonitors){
    this.carMonitor = carMonitor;
    carMonitor.registerObserver(this);
    }
}

然而,当我尝试在我的main方法中调用它时,我无法使用除一个参数之外的任何东西:

    BigCar bigCar = new BigCar();
    SmallCar smallCar = new SmallCar();
    AdvancedMonitor doubleAdvancedDisplay1 = new AdvancedMonitor();
    AdvancedMonitor doubleAdvancedDisplay2 = new AdvancedMonitor(bigCar);
    AdvancedMonitor doubleAdvancedDisplay3 = new AdvancedMonitor(bigCar, smallCar);

只有第二个有效。这是为什么?

enter image description here

它与我的界面有关吗?

public interface Subject {
    public void registerObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyObservers();
}
大车界面 - 现在的小型车基本相同:

public class BigCar implements Subject {
    private ArrayList observers;
    private int state;

    public BigCar(){
        observers = new ArrayList();
    }

    public void registerObserver(Observer o){
        observers.add(o);
    }

    public void removeObserver(Observer o){
        int i = observers.indexOf(o);
        if (i >= 0){
            observers.remove(i);
        }
    }

    public void notifyObservers(){
        for (int i = 0; i < observers.size(); i++){
            Observer observer = (Observer)observers.get(i);
            observer.update(state);
        }
    }

    public void stateChanged() {
        notifyObservers();
    }

    public void setState(int state){
        this.state = state;
        stateChanged();
    }
}

2 个答案:

答案 0 :(得分:1)

我写下面的代码:

public class Test {
    public static class AdvancedMonitor {
        private String carMonitor;

        public AdvancedMonitor(String... carMonitors) {
            for (String carMonitor : carMonitors) {
                this.carMonitor = carMonitor;
                System.out.println(this.carMonitor);
            }
        }
    }

    public static void main(String[] args) {
        String bigCar = "bigCar";
        String smallCar = "smallCar";
        System.out.println("step 1");
        AdvancedMonitor doubleAdvancedDisplay1 = new AdvancedMonitor();
        System.out.println();
        System.out.println("step 2");
        AdvancedMonitor doubleAdvancedDisplay2 = new AdvancedMonitor(bigCar);
        System.out.println();
        System.out.println("step 3");
        AdvancedMonitor doubleAdvancedDisplay3 = new AdvancedMonitor(bigCar, smallCar);
    }
}

我有以下结果:

step 1

step 2
bigCar

step 3
bigCar
smallCar

在我看来,一切都正确。你的情况怎么了?您是否使用loggingSystem.out.println调试问题?看起来你的问题不是Java varagrs,但你在carMonitor.registerObserver(this)中有一些例外。

P.S。另外,您了解每个AdvancedMonitor只有一个可变的carMonitor?在结果中使用new AdvancedMonitor(bigCar, smallCar);,只有private String carMonitor;中的smallCar才能使用AdvancedMonitor?

P.P.S。在construstor中使用this也是个坏主意,因为在运行construstor时对象并不真正创建。

答案 1 :(得分:0)

实际上构造函数有效。

请查看以下陈述:
SmallCar和BigCar都实现了主题
类AdvancedMonitor实现Observer
AdvancedMonitor doubleAdvancedDisplay未多次声明,但在您的代码中却是。它应该像:

AdvancedMonitor doubleAdvancedDisplay1 = new AdvancedMonitor();
AdvancedMonitor doubleAdvancedDisplay2 = new AdvancedMonitor(bigCar);
AdvancedMonitor doubleAdvancedDisplay3 = new AdvancedMonitor(bigCar, smallCar);

我希望它会帮助你