【问题标题】:can we have a main() in an interface and different implementations for main() in the classes that implement this interface?我们可以在一个接口中有一个 main() 并在实现这个接口的类中有不同的 main() 实现吗?
【发布时间】:2012-03-30 18:02:48
【问题描述】:

我知道 main() 可以在一个类中重载,编译器总是将带有String[] args 的类作为参数作为执行开始的主要方法。但是是否可以声明相同的

main(String args[]) in an interface and implement it in different classes differently?

例如,

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");
    }
}

【问题讨论】:

    标签: java main overriding


    【解决方案1】:

    这是一个编译器错误。你不能用静态方法覆盖非静态接口

    【讨论】:

      【解决方案2】:

      我认为你遗漏了一些东西。静态方法(如Testclass1Testclass2 中的主方法)不能覆盖子类方法。

      【讨论】:

      • 接口根本不能有static方法!
      • 我在哪里说过可以?天哪
      • 你也没说不能!
      【解决方案3】:

      不,你不能,因为main 必须是静态的才能用作入口点,并且接口不允许使用静态,直到Java 7

      是的,如果您在Java 8 中工作,您可以在界面中运行psvm。因为从 Java 8 开始的接口中允许使用静态方法。

      当然,您不能覆盖main 方法,因为psvm 是一个静态方法。

      【讨论】:

      • 还有更多的答案。 ;)
      • psvm = public static void main
      【解决方案4】:

      您的问题有两个答案。

      1. 首先,Interface 中不能有 static 方法
      2. 是的,你可以重载main() 方法,但是当你启动你的类时,只有 public static void main(String args[]){} 方法将被视为入口点。

      例如

      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");
          }
      }
      

      当您启动上述课程时,输出将是“entry point

      【讨论】:

      • +1 for "you can't have static methods in an Interface",因为这是这个问题的唯一答案。
      • 你可以在 Java 8 中,@Lion
      【解决方案5】:

      我不确定但是您可以在多个类(不是同一个类)中拥有多个主要方法。 我们以要运行其主要方法的类的名称启动程序。因此,之后 JVM 将仅在该类中查找 main 方法。所以应该没有问题。

      我不确定,如果我错了,请告诉我。

      【讨论】:

        【解决方案6】:

        您可以创建自己的启动机制,但我不确定您为什么要这样做。

        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();
        }
        

        顺便说一句:你不必有一个 main() 方法,例如

        class NoMain {
            static {
               System.out.println("Hello World");
               System.exit(0);
            }
        }
        

        编译并运行没有错误。

        【讨论】:

          【解决方案7】:

          您在 Testclass1 中将 main(String[] args) 声明为静态,但在接口“test”中它是非静态的,并且接口不允许 main(String[] args) 作为静态。即使您覆盖 main(String [] args) 在 Testcalss1 中作为非静态是不允许的,因为 main(String args) 已经在 Testclass1 中定义。所以你不能在接口中声明 main(String[] args)。你又犯了一个错误是初始化接口测试为 t。你不能那样做。

          【讨论】:

            【解决方案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 !!");
                }
            }
            

            【讨论】:

              【解决方案9】:

              main() 是静态的。所以,我们不能覆盖静态方法。接口允许抽象方法方法,它们将在子类中实现。所以,抽象方法永远不会是静态的。最后我得出结论 main() 不可能执行在接口中。

              【讨论】:

                【解决方案10】:

                答案:是的,我们可以提供在接口中声明的 main() 的不同实现,以及通过覆盖方法实现该接口的类,并且如果在接口中定义,我们可以重载静态 main 方法。

                有关 Java 8 中接口更改的更多信息。

                在 Java 8 之前,无法在接口内定义方法。

                但是 Java 8 对接口进行了更改,可以在接口内定义默认方法和静态方法。因此,下面的代码可以正常工作,没有任何问题。

                public interface TestInterfaces {
                    public static void main(String[] a){    
                        System.out.println("I am a static main method inside Inteface !!");
                    }
                }
                

                有关此功能的信息,请参阅以下链接: http://www.journaldev.com/2752/java-8-interface-changes-static-method-default-method

                【讨论】:

                  【解决方案11】:

                  您可以在 java 8 的接口中编写静态 main 方法,但不能覆盖它,因为它是静态方法。

                  【讨论】:

                    【解决方案12】:

                    希望这个例子能有所帮助:

                    要访问接口静态方法,我们需要使用接口名称:来自 java 1.8

                    interface A {
                        public static void main(String[] args) {
                            System.out.println("Interface main method");
                        }
                    }
                    public class MyClass {
                        public static void main(String args[]) {
                            A.main(new String[]{});
                            System.out.println("Main main");
                        }
                    }
                    
                    输出:

                    接口主方法

                    主要主要

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2010-10-13
                      • 1970-01-01
                      • 1970-01-01
                      • 2020-07-08
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多