问题
I know it's legal to have a main method in an abstract class, because Eclipse allows me to do the following and run the class as a java application. But does it make sense to do something like this?
Is there a real world scenario where one would need to have a main method in an abstract class?
public abstract class Automobile
{
public Boolean powerOn()
{
// generic implementation for powering on an automobile
return true;
}
public void move()
{
// generic implementation for move
}
public void changeDirection(String newDir)
{
// generic implementation for changing direction
}
public abstract void accelerate(Integer changeInSpeed);
public abstract Integer refuel(Integer inputFuel);
public static void main(String[] args)
{
System.out.println("I am a main method inside an abstract Automobile class");
}
}
回答1:
No, not really. Trying to create a class hierarchy where different classes share the same main method doesn't seem useful. See this example:
public abstract class A {
public static void doStuff() {
System.out.println("A");
}
public static void main(String[] args) {
System.out.println("starting main in A");
doStuff();
}
}
class B extends A {
public static void doStuff() {
System.out.println("B");
}
}
this prints out
c:\Users\ndh>java A
starting main in A
A
which is expected but boring, and
c:\Users\ndh>java B
starting main in A
A
which doesn't do what you want.
Shadowing static method calls doesn't work like virtual overriding of instance methods, you have to start with explicitly naming the class to use as your starting point for finding the right method signature (or by default you get the class where the call is made). The problem is that the main method can't know about the subclasses (or what's the point of putting it on the abstract class), so there's no good way to get subclass-specific information into the superclass main method.
My preference is to minimize my use of the static keyword, reserving it for constants (although not so much since enums came out) and stateless functions with no dependencies. Favor object-oriented techniques instead. With static methods you have to be specific. With OO the idea is you can avoid being specific and let the subclasses take care of themselves.
回答2:
You are asking about a scenario with a need for the main
method to be in an abstract
class, but I could ask the other way round: is there ever a need to have a main
method in a non-abstract
class?
Obviously, it’s completely irrelevant whether the application entry point is within an abstract
class or not. What matters:
- That class must be
public
- It should have a name that is useful for the outside world as it will appear in non-Java context, i.e. command line, start scripts or XML files, documentation, etc.
That said, if you have an application which consists of one public abstract
base class with a concise name (like Automobile
) and some implementation classes which are not public
and/or have hard to remember names (like AutomobileXF838EngineImpl
), there is a point in choosing that base class for hosting the application entry point.
But this applies to rather small applications only, where the number of classes matters. For larger applications you will usually have a dedicated class just to serve as application entry point hosting the main
method. There might be even multiple start classes used for different environments or frameworks.
So these start classes are neither, an abstract
base class nor an implementation class of such a base class, but completely unrelated to such class hierarchies. Since they are not base classes of a type hierarchy, they are usually not abstract
.
回答3:
you can create objects of another class and use other class methods by composition
class Test1 {
int x = 10;
public void display() {
System.out.println("Hello! This is Test1 class");
}
}
public abstract class Test {
public static void main(String args[]) {
Test1 t1 = new Test1();
System.out.println("From abstract class main(): " + t1.x);
t1.display();
}
}
来源:https://stackoverflow.com/questions/31593307/having-main-method-in-an-abstract-class