Why can't I define a static method in a Java interface?

后端 未结 24 1502
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 05:44

EDIT: As of Java 8, static methods are now allowed in interfaces.

Here\'s the example:

public interface IXMLizable         


        
24条回答
  •  轮回少年
    2020-11-22 06:27

    Suppose you could do it; consider this example:

    interface Iface {
      public static void thisIsTheMethod();
    }
    
    class A implements Iface {
    
      public static void thisIsTheMethod(){
        system.out.print("I'm class A");
      }
    
    }
    
    class B extends Class A {
    
      public static void thisIsTheMethod(){
        System.out.print("I'm class B");
      } 
    }
    
    SomeClass {
    
      void doStuff(Iface face) {
        IFace.thisIsTheMethod();
        // now what would/could/should happen here.
      }
    
    }
    

提交回复
热议问题