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

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

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

问题

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

import java.util.Random;

interface SharedConstants {
    int NO = 0;
    int YES = 1;
    int LATER = 3;
    int SOON = 4;
    int NEVER = 5;
}

class Question implements SharedConstants {
    Random rand = new Random();

    int ask() {
        int prob = (int) (100 * rand.nextDouble());
        if(prob < 30) return NO;
        else if(prob < 60) return YES;
        else if(prob < 75) return LATER;
        else if(prob < 98) return SOON;
        else return NEVER;
    }
}

public class AskMe implements SharedConstants {
    static void answer(int result) {
        switch(result) {
            case NO:
                System.out.println("No");
                break;
            case YES:
                System.out.println("Yes");
                break;
            case LATER:
                System.out.println("Later");
                break;
            case SOON:
                System.out.println("Soon");
                break;
            case NEVER:
                System.out.println("Never");
                break;
        }
    }

    public static void main(String[] args) {
        Question q = new Question();
        answer(q.ask());
        answer(q.ask());
        answer(q.ask());
        answer(q.ask());
    }
}

对于您提出的关于在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.

import java.util.Random;
interface SharedConstants {
int NO = 0;
int YES = 1;
int LATER = 3;
int SOON = 4;
int NEVER = 5;
}
class Question implements SharedConstants {
Random rand = new Random();
int ask() {
int prob = (int) (100 * rand.nextDouble());
if(prob &lt; 30) return NO;
else if(prob &lt; 60) return YES;
else if(prob &lt; 75) return LATER;
else if(prob &lt; 98) return SOON;
else return NEVER;
}
}
public class AskMe implements SharedConstants {
static void answer(int result) {
switch(result) {
case NO:
System.out.println(&quot;No&quot;);
break;
case YES:
System.out.println(&quot;Yes&quot;);
break;
case LATER:
System.out.println(&quot;Later&quot;);
break;
case SOON:
System.out.println(&quot;Soon&quot;);
break;
case NEVER:
System.out.println(&quot;Never&quot;);
break;
}
}
public static void main(String[] args) {
Question q = new Question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
}
}

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。就像这样:

void answer(int result) {
    switch(result) {
        case NO:
            System.out.println("No");
            break;
        case YES:
            System.out.println("Yes");
            break;
        case LATER:
            System.out.println("Later");
            break;
        case SOON:
            System.out.println("Soon");
            break;
        case NEVER:
            System.out.println("Never");
            break;
    }
}

public static void main(String[] args) {
    Question q = new Question();
    AskMe askMe = new AskMe();
    askMe.answer(q.ask());
    askMe.answer(q.ask());
    askMe.answer(q.ask());
    askMe.answer(q.ask());
}
英文:

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

void answer(int result) {
switch(result) {
case NO:
System.out.println(&quot;No&quot;);
break;
case YES:
System.out.println(&quot;Yes&quot;);
break;
case LATER:
System.out.println(&quot;Later&quot;);
break;
case SOON:
System.out.println(&quot;Soon&quot;);
break;
case NEVER:
System.out.println(&quot;Never&quot;);
break;
}
}
public static void main(String[] args) {
Question q = new Question();
AskMe askMe = new AskMe();
askMe.answer(q.ask());
askMe.answer(q.ask());
askMe.answer(q.ask());
askMe.answer(q.ask());
}

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:

确定