在interface 中的方法也可以有方法体
public interface InterfaceMethod { public static int ID=1;//默认为 final static 即常量 //public InterfaceMethod(){} public static void say(){ System.out.println("aa"); } public default void sayhi(){ System.out.println("aa"); } public void sayBye(); }
public class IntefaceMethodImpl implements InterfaceMethod { @Override public void sayBye() { // TODO Auto-generated method stub System.out.println("syabye--"); } }
public class TestInterfaceMethod { public static void main(String[] args) { InterfaceMethod ifm=new IntefaceMethodImpl(); ifm.sayBye(); ifm.sayhi(); //ifm.ID=3; System.out.println(ifm.ID); System.out.println(InterfaceMethod.ID); InterfaceMethod.say(); //IntefaceMethodImpl.say(); 错误 } }