简体   繁体   English

我是否必须声明一个类为公开类(这是程序中没人会使用的唯一类)

[英]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.

相关问题 java 程序只有一个公共 class 应该主要方法只是该公共 class 的一部分,然后是该程序的任何其他 ZA2F2ED4F8DCEBC2CBBDZC21? - java program have only one public class should main method only be part of that public class then any other class of that program? 我可以在公共课程中声明扫描程序吗? - Can I declare a Scanner in a public class? 为什么我不能将obj类声明为public? - Why I cannot declare obj class as public? 我是否应该在Service类上使用@Transactional - Should I ever use @Transactional on a Service class 为什么必须使用Object类作为引用类型才能使该程序正常工作? - Why do I have to use the Object class as a reference type to make this program work? 声明 Class 属性受保护还是公共? - Declare Class Attribute Protected or Public? 如何在此类中使用公共变量和方法? - How do I use the public variables and methods in this class? 如果该类只有一个构造函数,我应该在该类中声明变量吗? - Should I declare variables in the class, if the class has only a constructor? 如何正确实例化类中的公共类+数据结构,以便其他对象可以使用它? - How do I correctly instantiate a public class+data structure in a class so that other objects can use it? 我如何声明一个实现了可限制类类型的接口的类? - How do I declare a class that implements an interface that constrains the type of a class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM