为什么我们需要像下面这个方法一样的访问修饰符 “static”?

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

Why does we need access modifier "static" for some methods like the following one?

问题

以下是从《Java编程完全参考》(作者:Herbert Schildt)第239页和240页PDF中提取的代码。我已经翻译了您提供的代码部分:

  1. import java.util.Random;
  2. interface SharedConstants {
  3. int NO = 0;
  4. int YES = 1;
  5. int LATER = 3;
  6. int SOON = 4;
  7. int NEVER = 5;
  8. }
  9. class Question implements SharedConstants {
  10. Random rand = new Random();
  11. int ask() {
  12. int prob = (int) (100 * rand.nextDouble());
  13. if(prob < 30) return NO;
  14. else if(prob < 60) return YES;
  15. else if(prob < 75) return LATER;
  16. else if(prob < 98) return SOON;
  17. else return NEVER;
  18. }
  19. }
  20. public class AskMe implements SharedConstants {
  21. static void answer(int result) {
  22. switch(result) {
  23. case NO:
  24. System.out.println("No");
  25. break;
  26. case YES:
  27. System.out.println("Yes");
  28. break;
  29. case LATER:
  30. System.out.println("Later");
  31. break;
  32. case SOON:
  33. System.out.println("Soon");
  34. break;
  35. case NEVER:
  36. System.out.println("Never");
  37. break;
  38. }
  39. }
  40. public static void main(String[] args) {
  41. Question q = new Question();
  42. answer(q.ask());
  43. answer(q.ask());
  44. answer(q.ask());
  45. answer(q.ask());
  46. }
  47. }

对于您提出的关于在AskMe类中创建answer方法的问题,为什么我们需要使用“static”访问控制,如果不使用,编译器会报错,错误信息类似于“error: non-static method answer(int) cannot be referenced from a static context”。

提前感谢您的帮助。

英文:

Here is the code I got from the book "Java: The complete reference" by Herbert Schildt on page 239, 240 as PDF. I have researched about "static" but in this case, I wonder why static must be used.

  1. import java.util.Random;
  2. interface SharedConstants {
  3. int NO = 0;
  4. int YES = 1;
  5. int LATER = 3;
  6. int SOON = 4;
  7. int NEVER = 5;
  8. }
  9. class Question implements SharedConstants {
  10. Random rand = new Random();
  11. int ask() {
  12. int prob = (int) (100 * rand.nextDouble());
  13. if(prob &lt; 30) return NO;
  14. else if(prob &lt; 60) return YES;
  15. else if(prob &lt; 75) return LATER;
  16. else if(prob &lt; 98) return SOON;
  17. else return NEVER;
  18. }
  19. }
  20. public class AskMe implements SharedConstants {
  21. static void answer(int result) {
  22. switch(result) {
  23. case NO:
  24. System.out.println(&quot;No&quot;);
  25. break;
  26. case YES:
  27. System.out.println(&quot;Yes&quot;);
  28. break;
  29. case LATER:
  30. System.out.println(&quot;Later&quot;);
  31. break;
  32. case SOON:
  33. System.out.println(&quot;Soon&quot;);
  34. break;
  35. case NEVER:
  36. System.out.println(&quot;Never&quot;);
  37. break;
  38. }
  39. }
  40. public static void main(String[] args) {
  41. Question q = new Question();
  42. answer(q.ask());
  43. answer(q.ask());
  44. answer(q.ask());
  45. answer(q.ask());
  46. }
  47. }

I wonder at the line that created answer method in class AskMe. Why do we need the "static" access control? If not, the compiler will give error like "error: non-static method answer(int) cannot be referenced from a static context".

Thanks in advance.

(This is my first time asking question, if any mistakes, tell me)

答案1

得分: 0

可以,但是没有static,你需要一个AskMe的实例在main中调用answer。就像这样:

  1. void answer(int result) {
  2. switch(result) {
  3. case NO:
  4. System.out.println("No");
  5. break;
  6. case YES:
  7. System.out.println("Yes");
  8. break;
  9. case LATER:
  10. System.out.println("Later");
  11. break;
  12. case SOON:
  13. System.out.println("Soon");
  14. break;
  15. case NEVER:
  16. System.out.println("Never");
  17. break;
  18. }
  19. }
  20. public static void main(String[] args) {
  21. Question q = new Question();
  22. AskMe askMe = new AskMe();
  23. askMe.answer(q.ask());
  24. askMe.answer(q.ask());
  25. askMe.answer(q.ask());
  26. askMe.answer(q.ask());
  27. }
英文:

You could, but without static you would need an instance of AskMe to call answer in main. Like,

  1. void answer(int result) {
  2. switch(result) {
  3. case NO:
  4. System.out.println(&quot;No&quot;);
  5. break;
  6. case YES:
  7. System.out.println(&quot;Yes&quot;);
  8. break;
  9. case LATER:
  10. System.out.println(&quot;Later&quot;);
  11. break;
  12. case SOON:
  13. System.out.println(&quot;Soon&quot;);
  14. break;
  15. case NEVER:
  16. System.out.println(&quot;Never&quot;);
  17. break;
  18. }
  19. }
  20. public static void main(String[] args) {
  21. Question q = new Question();
  22. AskMe askMe = new AskMe();
  23. askMe.answer(q.ask());
  24. askMe.answer(q.ask());
  25. askMe.answer(q.ask());
  26. askMe.answer(q.ask());
  27. }

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

发表评论

匿名网友

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

确定