英文:
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 < 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());
}
}
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("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());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论