jdk1.8新特性 : 接口中可以有普通方法(非静态方法)和静态方法 , 颠覆了之前我的理解 : 接口中只能有共有常量和抽象方法的概念,后面必须要加一句jdk1.7和1..7之前

看到jdk某些接口中存在default方法,于是...

http://shaomeng95.iteye.com/blog/998820    为什么接口只能是公有常量?

public interface Jdk8新特性 {
    public static final String AA = "hhe";
    
    default void test(){
        System.out.println("哈哈");
    }
    
    public static void hehe(){
        System.out.println("没什么");
    }
}


public class TestMain implements Jdk8新特性 {
    public static void main(String[] args) {
        TestMain tm = new TestMain();
        tm.test();   //接口中的default方法会继承过来
    }
    public static void hehe(){
        System.out.println("哈哈");
    }
}


1 如果TestMain在实现一个接口,这个接口中也有test这个方法且是default修饰的,那么会出现什么问题?
 编译器会让你重写其中一个方法,否则不能通过编译
public class TestMain implements Jdk8新特性, Jdk8新特性2 {
    public static void main(String[] args) {
        TestMain tm = new TestMain();
        tm.test();
    }
    public static void hehe(){
        System.out.println("哈哈");
    }
    @Override
    public void test() {
        Jdk8新特性2.super.test();
    }
}

2 实现类中定义一个一样的静态方法有影响吗?
 没有影响,静态方法与类(接口)相关

  

posted @ 2018-01-10 14:55  李勇888  阅读(1545)  评论(0编辑  收藏  举报