[英]Do I have to declare a class public (it's the only class in a program nobody will ever use)
I'm a beginning computer science student going through the Java gauntlet. 我是一名刚开始学习Java技巧的计算机科学专业的学生。 We had to write a program with an error in it;
我们必须编写一个有错误的程序。 it was a fun exercise actually.
这实际上是一个有趣的练习。 One of my classmates emailed me a patch(in English not diff) and said that he add
public
to the class. 我的一位同学通过电子邮件向我发送了一个补丁(英语中没有diff),并说他在班上添加了
public
。
This made me wonder. 这让我感到奇怪。 Does it matter or is it just a convention to declare the class
public
. 是重要的还是将类声明为
public
是一个惯例。 The program worked and from what I understand the default is package-private which I think would be best for crappy little scripts that CS students are mailing around to each other. 该程序正常工作,据我了解,默认值是程序包私有的 ,我认为这对于CS学生相互邮寄的笨拙的小脚本是最好的。
Can anybody give me some more insight into this—in regards to Java mostly but general CS theory might prove insightful as well—and maybe some terms that I can use to research these concepts further. 任何人都可以给我一些更多的见解-就Java而言,但一般的CS理论也可能很有见地-也许我可以使用一些术语来进一步研究这些概念。
I want to know if my classmates correction is valid and or important and why. 我想知道我的同学改正是否有效和/或重要,以及为什么。
EDIT 编辑
sorry here's my original program without an error 抱歉,这是我的原始程序,没有错误
import java.util.Scanner;
/**
* Universal hello translator
* Author: Noel Niles mail: noelniles@gmail.com
*
*/
class HelloUniverse {
public static void main (String [] args)
{
/* code */
int country; //number of country from list
String name; //name of person
String greeting; //hello in different languages
Scanner key = new Scanner(System.in);
System.out.print("Where are you from?\n"+
"Enter a number from the list:\n");
//TODO(anyone): Add some more countries and greetings
System.out.print("1. Afganistan\n" +
"2. Antarctica\n" +
"3. Australia\n" +
"4. Austria\n" +
"5. Bangladesh\n" +
"6. Belgium\n" +
"7. Brazil\n" +
"8. Burma\n" +
"9. Canada\n" +
"10. Chile\n" +
"11. China\n" );
//get the country code
country = key.nextInt();
key.nextLine();
//get the users name
System.out.println("What is you name?");
name = key.nextLine();
switch (country) {
case 1: greeting = "salaam";
break;
case 2: greeting = "h-h-h-i-i thththththere";
break;
case 3: greeting = "G'day mate";
break;
case 4: greeting = "Gruss Gott";
break;
case 5: greeting = "nomoskar";
break;
case 6: greeting = "Hallo";
break;
case 7: greeting = "Ola";
break;
case 8: greeting = "mingalaba";
break;
case 9: greeting = "Good day eh";
break;
case 10: greeting = "Hola";
break;
case 11: greeting = "Nei hou";
break;
default: greeting = "Invalid country";
break;
}
//display the greeting and the users name
System.out.printf("%s %s\n", greeting, name);
}
}
No, it's fine for the class containing the main
method to be non-public. 不,包含
main
方法的类可以是非公开的。 (It can even be a private nested class if you really want.) (如果您确实需要,它甚至可以是一个私有嵌套类。)
The main
method itself has to be public, but the class doesn't. main
方法本身必须是公共的,但类不是。
(Note that there's no "general CS theory" here - each language and platform has its own conventions and rules here.) (请注意,这里没有“通用CS理论”,每种语言和平台在这里都有自己的约定和规则。)
(I would strongly suggest using an array of greetings instead of a giant switch statement, admittedly... but that's a different matter.) (诚然,我强烈建议您使用问候语数组而不是巨大的switch语句,但这是另一回事。)
When your class does not have any modifier, it means that this class will still be visible in the same package or in your class. 如果您的类没有任何修饰符,则意味着该类在同一包或您的类中仍然可见。 See the different access levels here : http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
在此处查看不同的访问级别: http : //docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.