将命名参数传递给一个方法。

huangapple go评论77阅读模式
英文:

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());
    }
}

Stack trace:

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

What does this mean, and how can I correct it?
英文:

Code:

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());
	}
}

Stack trace:

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

What does this mean, and how can I correct it?

答案1

得分: 5

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

a.changeTheHueOfTheColor(1);
// 这里 -----------------^
英文:

Java does not have named arguments, just positional arguments. You need to pass it without the parameter's name:

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

huangapple
  • 本文由 发表于 2020年4月4日 16:44:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61025692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定