Java 我们可以在一个接口中有一个 main() 并且在实现这个接口的类中有 main() 的不同实现吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9699247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can we have a main() in an interface and different implementations for main() in the classes that implement this interface?
提问by Galaxin
I know that main() can be overloaded in a class with the compiler always taking the one with String[] args
as arguments as the main method from where the execution starts. But is it possible to declare the same
我知道 main() 可以在一个类中重载,编译器总是将带有String[] args
参数的那个作为执行开始的主要方法。但是是否可以声明相同
main(String args[]) in an interface and implement it in different classes differently?
For example,
例如,
package test;
interface test
{
public void main(String args[]);
public void display();
}
package test;
class Testclass1 implements test
{
public void display()
{
System.out.println("hello");
}
public static void main(String[] args)
{
test t;
t.display();
}
}
package temp;
import test.*;
abstract class Testclass2 implements test
{
public static void main(String args[])
{
System.out.println("TESTING");
}
}
采纳答案by UmNyobe
No you cannot, because main
has to be static in order to be used as an entry point, and Interfaces dont allow the use of static, until Java 7
.
不,你不能,因为main
必须是静态的才能用作入口点,并且接口不允许使用静态,直到Java 7
.
Yes you can run a psvm
in an interface, if you're working in Java 8
. Because static methods are allowed in an interface starting from Java 8.
是psvm
的,如果您在Java 8
. 因为从 Java 8 开始,接口中允许使用静态方法。
But of course, you cannot override the main
method, since psvm
is a static method.
但是当然,您不能覆盖该main
方法,因为它psvm
是一个静态方法。
回答by digitebs
this is a compiler error. you cant override a non static interface a static method
这是一个编译器错误。您不能覆盖非静态接口静态方法
回答by Johannes
I think you are missing something. Static methods (like the main method in Testclass1
and Testclass2
) cannot override subclass methods.
我认为你错过了一些东西。静态方法(如Testclass1
and 中的 main 方法Testclass2
)不能覆盖子类方法。
回答by Rakesh
There are two answers for your question.
你的问题有两个答案。
- First of all, you can't have
static
methods in anInterface
- Yes you can overload
main()
method, but when you launch your class, only thepublic static void main(String args[]){}
method will be treated as an entry point.
- 首先,你不能
static
在一个Interface
- 是的,您可以重载
main()
方法,但是当您启动类时,只有该public static void main(String args[]){}
方法会被视为入口点。
For example
例如
public class Test {
public static void main(String[] args) {
System.out.println("entry point)");
}
public static void main(String arg1) {
System.out.println("overload2");
}
public static void main(String arg1, String arg2) {
System.out.println("overload3");
}
}
When you launch the above class, the out will be "entry point
"
当您启动上述课程时,输出将是“ entry point
”
回答by me_digvijay
I am not sure But You can have multiple main methods in multiple classes (not the same class). We start the program with the name of the class whose main method we want to run. So after that JVM will look for main method in that class only. So there should not be any problem.
我不确定但是您可以在多个类(不是同一个类)中有多个主要方法。我们以要运行其主要方法的类的名称启动程序。所以在那之后 JVM 将只在该类中查找 main 方法。所以应该没有任何问题。
I am not sure so please let me know if I am wrong.
我不确定所以如果我错了,请告诉我。
回答by Peter Lawrey
You can create you own startup mechanism, but I am not sure why you would want to.
您可以创建自己的启动机制,但我不确定您为什么要这样做。
public class RunTests {
public static void main(String... args) throws ClassNotFoundException {
List<Class> classes = new ArrayList<Class>();
try {
for (String arg : args) {
classes.add(Class.forName(arg));
}
} catch (ClassNotFoundException e) {
if (classes.isEmpty())
throw e;
}
String[] remainingArgs = Arrays.asList(args).subList(classes.size(), args.length).toArray(new String[0]);
for(Class clazz: classes) {
try {
Test test = (Test) clazz.newInstance();
test.main(remainingArgs);
test.display();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
interface Test {
public void main(String... args);
public void display();
}
BTW: You don't have to have a main() method, e.g.
顺便说一句:你不必有一个 main() 方法,例如
class NoMain {
static {
System.out.println("Hello World");
System.exit(0);
}
}
compiles and runs without error.
编译并运行没有错误。
回答by mohan.reddy8
you declared a main(String[] args) as static in Testclass1,but in interface "test" it is non-static and interface not allow main(String[] args) as static .even if you override main(String[] args) in Testcalss1 as non-static it's not allowed, because main(String args) is already defined in Testclass1.so you cannot declare main(String[] args) in interface.And u done a one more mistaken is initializing the interface test as t.You cannot do that.
您在 Testclass1 中将 main(String[] args) 声明为静态,但在接口“test”中它是非静态的,并且接口不允许 main(String[] args) 为静态。即使您覆盖 main(String[] args) ) 在 Testcalss1 中作为非静态它是不允许的,因为 main(String args) 已经在 Testclass1 中定义。所以你不能在 interface.and 中声明 main(String[] args) 并且你做了一个更错误的是将接口测试初始化为t.你不能那样做。
回答by Nitin Dandriyal
With Java-8 you can have main method defined inside the interface, Below code will work in Java-8.
使用 Java-8,您可以在接口内定义 main 方法,下面的代码将在 Java-8 中工作。
public interface TestInterfaces {
public static void main(String[] a){
System.out.println("I am a static main method inside Inteface !!");
}
}
回答by Teja iskala
main() is static.so, we cant override static methods.Interfaces allows abstract methods methods & they will be implemented in subclasses.So, abstract methods never be static.finally I concluded that main() is not possible to execute in interfaces.
main() 是静态的。所以,我们不能覆盖静态方法。接口允许抽象方法方法,它们将在子类中实现。所以,抽象方法永远不会是静态的。最后我得出结论,main() 不可能在接口中执行。
回答by Kris Rudra
Answer : Yes, we can provide different implementation of main() declared in an interface and classes implementing that interface by overriding method and can overload static main method if defined in an interface.
答:是的,我们可以提供在接口中声明的 main() 的不同实现以及通过覆盖方法实现该接口的类,并且如果在接口中定义,则可以重载静态 main 方法。
Some more information regarding interface changes in Java 8.
有关 Java 8 中接口更改的更多信息。
Prior to Java 8, it was not possible to DEFINE methods inside interface.
在 Java 8 之前,不可能在接口内定义方法。
But there are changes made to Java 8 with respect to interface where one can DEFINE default and static method inside an interface. Hence below code will work fine without any issue.
但是在接口方面对 Java 8 进行了更改,可以在接口内定义默认方法和静态方法。因此,下面的代码可以正常工作,没有任何问题。
public interface TestInterfaces {
public static void main(String[] a){
System.out.println("I am a static main method inside Inteface !!");
}
}
For information regarding this features, please refer below link: http://www.journaldev.com/2752/java-8-interface-changes-static-method-default-method
有关此功能的信息,请参阅以下链接:http: //www.journaldev.com/2752/java-8-interface-changes-static-method-default-method