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

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

Pass named parameter to a method

问题

  1. class AllTheColorsOfTheRainbow {
  2. private int hue = 0;
  3. int anIntegerRepresentingColors;
  4. void changeTheHueOfTheColor(int newHue) {
  5. this.hue = newHue;
  6. }
  7. public int getHue(){
  8. return this.hue;
  9. }
  10. }
  11. public class Ex11 {
  12. public static void main(String [] args){
  13. AllTheColorsOfTheRainbow a = new AllTheColorsOfTheRainbow();
  14. a.changeTheHueOfTheColor(newHue = 1);
  15. System.out.println(a.getHue());
  16. }
  17. }
  18. Stack trace:
  19. javac Ex11.java
  20. Ex11.java:18: error: cannot find symbol
  21. a.changeTheHueOfTheColor(newHue = 1);
  22. ^
  23. symbol: variable newHue
  24. location: class Ex11
  25. 1 error
  26. What does this mean, and how can I correct it?
英文:

Code:

  1. class AllTheColorsOfTheRainbow {
  2. private int hue = 0;
  3. int anIntegerRepresentingColors;
  4. void changeTheHueOfTheColor(int newHue) {
  5. this.hue = newHue;
  6. }
  7. public int getHue(){
  8. return this.hue;
  9. }
  10. }
  11. public class Ex11 {
  12. public static void main(String [] args){
  13. AllTheColorsOfTheRainbow a = new AllTheColorsOfTheRainbow();
  14. a.changeTheHueOfTheColor(newHue = 1);
  15. System.out.println(a.getHue());
  16. }
  17. }

Stack trace:

  1. javac Ex11.java
  2. Ex11.java:18: error: cannot find symbol
  3. a.changeTheHueOfTheColor(newHue = 1);
  4. ^
  5. symbol: variable newHue
  6. location: class Ex11
  7. 1 error

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

答案1

得分: 5

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

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

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

  1. a.changeTheHueOfTheColor(1);
  2. // 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:

确定