首页 > 其他 > 详细

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

时间:2018-01-10 15:06:53      阅读:334      评论:0      收藏:0      [点我收藏+]

看到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 实现类中定义一个一样的静态方法有影响吗?
 没有影响,静态方法与类(接口)相关

  

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

原文:https://www.cnblogs.com/liyong888/p/8258569.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!