有什么好处为公共类编写主要方法? [英] is there any benefit of writing public classes for the main method?

查看:140
本文介绍了有什么好处为公共类编写主要方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以编写一个包含一个main方法的非公共类的java程序 - 它将编译和运行很好。为什么人们把主类公开?

We can write a java program with one non-public class containing a main method -- it'll compile and run just fine. Why do people make the main class public?

有什么好处吗?

推荐答案

这个类没有什么好处,因为它有一个main方法。

There is no benefit of making the class public for the sake of it having a main method.

话虽如此,原因不是。机会是主要的类将是非常短,几乎没有,如果有的,实质性的方法,或它将被烘焙到一个核心类如

That being said, there isn't really much of a reason not to either. Chances are the main class is either going to be very short with few, if any, substantial methods in it, or it's going to be baked into one of the core classes like

class Server {
    public static void main(String[] args) {
        Server s = new Server();
        s.start();
    }
    // rest of Server class here
}

通常这些核心类是你想要公开的。

And typically those core classes are something you'd want to be public.

但这不是关于类是公开的好处。这是关于将类作为公共的好处,因为它具有主要方法,并且没有直接的好处。

But this isn't about the benefits of having classes be public. This is about the benefits of having a class be public because it has the main method and there are no direct benefits as a result of that.

这篇关于有什么好处为公共类编写主要方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆