在 Java 中从该类的 main 内部实例化对象

发布于 2024-12-12 06:22:38 字数 418 浏览 1 评论 0原文

我正在查看我的 OOP 类文档,发现了这个例子:

class Student {
    private String name;
    public int averageGrade;


    public Student(String n, int avg) {
        name = n;
        averageGrade = avg;
    }

    public static void main(String[] args) {
        Student s = new Student("John", 9);
    }
}

我发现它们正在从同一个类的 main 实例化一个对象,这让我感到困惑。这被认为是不好的做法吗?新创建的对象s会有main方法吗?

谢谢你!

I was looking through my OOP class documentation and I found this example:

class Student {
    private String name;
    public int averageGrade;


    public Student(String n, int avg) {
        name = n;
        averageGrade = avg;
    }

    public static void main(String[] args) {
        Student s = new Student("John", 9);
    }
}

I find it confusing that they are instantiating an object from the main of the same class. Is this considered bad practice? Will the newly created object s have a main method?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我一向站在原地 2024-12-19 06:22:38

这完全没有问题。这是完全正常的。 (诚​​然,对于具有 main 方法的类来说,显然可以执行 - Student 类中的 main 方法更有意义没有多大意义。)

对象实际上没有方法 - 有方法,要么是在没有任何特定上下文的情况下调用的静态方法,要么是在特定对象上调用的实例方法该类型(或子类)。

虽然您可以调用s.main(...),但它实际上只是解析为对静态方法Student.main的调用;你不应该像这样“通过”表达式调用静态方法,因为它很令人困惑。

There's nothing wrong at all with this. It's entirely normal. (Admittedly it would make more sense for a class with a main method to be something one could obviously execute - a main method in a Student class doesn't make as much sense.)

Objects don't really have methods - classes have methods, either static methods which are called without any particular context, and instance methods which are called on a particular object of that type (or a subclass).

While you could call s.main(...) that would actually just resolve to a call to the static method Student.main; you shouldn't call static methods "via" expressions like this, as it's confusing.

趁微风不噪 2024-12-19 06:22:38

不,这不是坏习惯。实际上这种情况相当频繁。您错过的是 main 是一个静态方法。它不是 Student 对象的方法。这是Student类的一个方法。您不能使用 someStudent.main(...) 调用它,而是使用 Student.main(...) 调用它。

请参阅 http://download.oracle.com/javase/tutorial/java/ javaOO/classvars.html 了解更多说明。

No, it's not bad practice. It's actually fairly frequent. What you missed is that main is a static method. It's not a method of the Student object. It's a method of the Student class. You don't invoke it with someStudent.main(...), but with Student.main(...).

See http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html for more explanations.

一影成城 2024-12-19 06:22:38

这很好。

我知道它看起来有点递归,但发生的情况是,当您从命令行启动此类时,会调用 main() 方法,然后当您实际实例化对象的实例时,会调用构造函数。 (另请参阅乔恩的评论。)

This is just fine.

I know it looks a bit recursive, but what happens is that the main() method gets called when you launch this class from the command line, and then the constructor gets called when you actually instantiate an instance of the object. (See Jon's comment as well.)

最近可好 2024-12-19 06:22:38

这既不是坏事,也不是好事。

这取决于用途。在您的示例中,构造函数是从定义为静态的 main() 方法调用的,因此您别无选择。

这种模式“良好”使用的另一个例子是工厂方法模式。
枚举也在 valueOf() 方法中使用了这种技术(这也是工厂方法的一个示例)。

It is neither bad nor good.

It depends on the usage. In your example the constructor is called from main() method that is static by definition, so you have no other choice.

Yet another example of "good" usage of this pattern is factory method pattern.
Enums use this technique too in valueOf() method (that is an example of factory method too).

Saygoodbye 2024-12-19 06:22:38

完全没问题 完全没问题...
如果类的成员被声明为静态,那么它就是一个实体,其生命方式与该类的任何特定对象不同。它可以被自身使用,而不需要使用任何对象。或者它在不同的对象之间很常见。您实际上可以通过在类中设置一个静态变量来计算从类创建的对象的数量,例如

 class   A
     {
         A()
          {
           count++
           }

     static count=0;
       --- 
       ---
      }

每次 A 的对象时,创建的计数都会加一。

由于静态方法不属于任何特定的对象,因此在类之外,它被称为
类名.method()
就像调用普通方法一样 classObject.method()

It's totally fineIt's totally fine...
If a member of a class is declared as static,it's an entity lives differently of any particular object of the class.Its something that can be used by itself,without using any objects. OR it's common between different objects.You can actually count the number of objects created from a class,by setting up a static variable in the class like

 class   A
     {
         A()
          {
           count++
           }

     static count=0;
       --- 
       ---
      }

And each time an object of A,created count will add one.

Since static methods does not belong to any objects particularly,Outside the class,its called as
classname.method()
just like ordinary methods are called as classObject.method()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文