在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();
}
InterfaceMethod
public class IntefaceMethodImpl implements InterfaceMethod {
    @Override
    public void sayBye() {
        // TODO Auto-generated method stub
        System.out.println("syabye--");
    }
}
IntefaceMethodImpl
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(); 错误
    }
}
测试

 

 posted on 2017-02-16 20:54  夏末秋萍  阅读(1116)  评论(0编辑  收藏  举报