main方法在抽象类中有什么用? [英] What is the use of main method in abstract class?

查看:196
本文介绍了main方法在抽象类中有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们可以在抽象类中编写main方法,但我们可以从中实现什么?

I know that we can write main method in abstract class, but what we can achieve from it ?

 public abstract class Sample
 {
         public static void main(String args[])
         {                        

            System.out.println("Abstract Class main method : ");

         }
 }

我们无法创建对象抽象类,那么抽象类中main方法的用途是什么?

We can not create the object of abstract class ,so what is the use of main method in abstract class ?

推荐答案


抽象只是意味着您无法直接实例化该类。

Abstract just means you can't instantiate the class directly.

加载类与创建类的实例不同。并且不需要创建类的实例来调用main(),因为它是静态的。所以没有问题。

Loading a class is not the same as creating an instance of the class. And there's no need to create an instance of the class to call main(), because it's static. So there's no problem.

抽象只是意味着你不能直接实例化这个类。如果需要,可以使用构造函数 - 子类可能需要它们来启动对象状态。你可以使用静态方法,包括main(),它们不需要一个对象,所以调用它们很好。

Abstract just means you can't instantiate the class directly. You can have constructors if you want - they might be needed for subclasses to initiate the object state. You can have static methods, including main() and they don't need an object so calling them is fine.


所以你只有尝试创建对象时出错,即遇到抽象限制时。

So you only got error when you try to create the object, which is when you run into the abstract limitation.

这篇关于main方法在抽象类中有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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