将命名参数传递给方法

Pass named parameter to a method

代码:

class AllTheColorsOfTheRainbow {
    private int hue = 0;
    
    int anIntegerRepresentingColors;    
    
    void changeTheHueOfTheColor(int newHue) {
        this.hue = newHue;
    }

    public int getHue(){
        return this.hue;
    }
}

public class Ex11 {
    public static void main(String [] args){
        AllTheColorsOfTheRainbow a = new AllTheColorsOfTheRainbow();
        a.changeTheHueOfTheColor(newHue = 1);
        System.out.println(a.getHue());
    }
}

堆栈跟踪:

 javac Ex11.java 
Ex11.java:18: error: cannot find symbol
        a.changeTheHueOfTheColor(newHue = 1);
                                 ^
  symbol:   variable newHue
  location: class Ex11
1 error

这是什么意思,我该如何更正它?

Java 没有命名参数,只有位置参数。您需要在没有参数名称的情况下传递它:

a.changeTheHueOfTheColor(1);
// Here -----------------^