一、introduce interface default method

Introduce default method
Write the default method at interface
The multiply conflict resolve

interface default method/static method

JDK1.8中为什么接口中要引入default方法?比如JDK以前的版本如JDK1.0 List接口:

public interface List<E>{
void add(E e);
}

其他的Commons/Guavaa等第三方实现了JDK的List接口,就要重写add方法。

jdk1.8之后在List等很多接口中都加入了stream()的获取方法:

default Stream<E> stream();

如果stream方法不是default的话,那么这些第三方的实现List的类统统都要加上stream()的实现,改动太大,jdk1.8为了让第三方的这些实现不需要改动,完美兼容,就将stream()等这些方法
设置为default,那些第三方的实现类就不需要做任何改动,就可以使用默认的stream方法,也可以重写它。

二、自己写个简单的含有default方法的interface

package com.cy.java8;

public class DefaultInAction {
public static void main(String[] args) {
A a = () -> 10; System.out.println(a.size()); //10
System.out.println(a.isEmpty());//false } @FunctionalInterface
private interface A{ int size(); default boolean isEmpty(){
return size() == 0;
}
}
}

三、一个类如果实现多个接口,多个接口中含有重复名字的方法,怎么解决冲突?

三大原则:

1.Classes always win:class的优先级是最高的。比如class C重写了hello方法,优先级最高。
2.Otherwise, sub-interface win:if B extends A, B is more specific than A.
3.Finally, if the choice is still ambiguous, 那么你自己就要重写了。C implements A, B 。A和B没有任何关系,那么C必须重写hello方法。

原则1对应的代码例子:

 package com.cy.java8;

 public class DefaultInAction {
public static void main(String[] args) {
B c = new C();
c.hello(); //C.hello
} private interface A{
default void hello(){
System.out.println("A.hello");
}
} private interface B extends A{
@Override
default void hello() {
System.out.println("B.hello");
}
} private static class C implements A, B{
@Override
public void hello() {
System.out.println("C.hello");
}
}
}

原则2对应的代码例子:

 package com.cy.java8;

 public class DefaultInAction {
public static void main(String[] args) {
A c = new C();
c.hello(); //B.hello
} private interface A{
default void hello(){
System.out.println("A.hello");
}
} private interface B extends A{
@Override
default void hello() {
System.out.println("B.hello");
}
} private static class C implements A, B{ }
}

原则3对应的代码例子:

 package com.cy.java8;

 public class DefaultInAction {
public static void main(String[] args) {
C c = new C();
c.hello(); //C.hello
} private interface A{
default void hello(){
System.out.println("A.hello");
}
} private interface B{
default void hello() {
System.out.println("B.hello");
}
} private static class C implements A, B{
@Override
public void hello() {
//A.super.hello();
//B.super.hello();
System.out.println("C.hello");
}
}
}

  

----

Interface default method介绍的更多相关文章

  1. 深入学习Java8 Lambda (default method, lambda, function reference, java.util.function 包)

    Java 8 Lambda .MethodReference.function包 多年前,学校讲述C#时,就已经知道有Lambda,也惊喜于它的方便,将函数式编程方式和面向对象式编程基于一身.此外在使 ...

  2. Application binary interface and method of interfacing binary application program to digital computer

    An application binary interface includes linkage structures for interfacing a binary application pro ...

  3. jvm Classload method介绍

    1,jvm Classload默认几个重要方法介绍 findClass:Finds and loads the class with the specified name from the URL s ...

  4. interface中定义default方法和static方法

    interface的default方法和static方法 接口中可以定义static方法,可通过接口名称.方法名()调用,实现类不能继承static方法: 接口中可以定义default方法,defau ...

  5. java 小记

    1.获取web项目根目录的绝对路径 request.getContextPath()  获取项目名称,如    /BiYeSheJi getServletContext().getRealPath(& ...

  6. Java 8新特性——default方法(defender方法)介绍

    我们都知道在Java语言的接口中只能定义方法名,而不能包含方法的具体实现代码.接口中定义的方法必须在接口的非抽象子类中实现.下面就是关于接口的一个例子: 1 2 3 4 5 6 7 8 9 10 11 ...

  7. 关于java8 interface的default方法

    转自鸟窝 博主写的挺详细,不了解的看一看啊 以前经常谈论的Java对比c++的一个优势是Java中没有多继承的问题. 因为Java中子类只能继承(extends)单个父类, 尽管可以实现(implem ...

  8. Override is not allowed when implementing interface method Bytecode Version Overriding and Hiding Methods

    java - @Override is not allowed when implementing interface method - Stack Overflow https://stackove ...

  9. Getting start with dbus in systemd (01) - Interface, method, path

    Getting start with dbus in systemd (01) 基本概念 几个概念 dbus name: connetion: 如下,第一行,看到的就是 "dbus name ...

随机推荐

  1. C++ GB2312 和 utf8 在win32下 互转

    string ANSItoUTF8(const char* strAnsi) { //获取转换为宽字节后需要的缓冲区大小,创建宽字节缓冲区,936为简体中文GB2312代码页 , NULL, NULL ...

  2. jQuery获取兄弟标签的文本

    // 一个div里面有一个span标签和多个button标签,每个button标签都有id,span标签没有id,通过点击其中一个button标签,来获取到span标签的text function ( ...

  3. Reflector破译

    一:安装: 这个在包里,自己安装 二:注册 1. 断网2. 运行.NET Reflector,点击Help -> Activate 3. 运行注册机,复制注册机生成的序列号,粘贴到.NET Re ...

  4. 牛客练习赛47 E DongDong数颜色 (树上启发式合并)

    链接:https://ac.nowcoder.com/acm/contest/904/E 来源:牛客网 DongDong数颜色 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...

  5. HDU - 6087 Rikka with Sequence (可持久化treap+倍增+重构)

    题目链接 感谢Dream_Lolita的题解,经过无数次失败的尝试之后终于AC了... 线段树是维护区间信息的强大工具,但它的形态是固定的,只支持修改和删除操作,不支持插入.反转.复制.分裂合并等操作 ...

  6. iview 在Table组件中使用switch组件并自定义内容和增加自定义事件

    注意: 使用render函数. 效果: 添加自定义文字: 1. 2. 上面的两种方法效果都是一样的: 增加自定义事件: 结果: 自信是进步的源泉. 继续加油. ^_^

  7. css 设置头像图片不变形

    css 设置头像图片不变形 在样式中加 object-fit: cover 就可以了

  8. js-点击+加关注变成已关注,已关注状态时,鼠标滑动上的状态时取消关注

    效果: HTML: <div class="rightBtn cur">+关注</div> CSS: .rightBtn{ width: 80px; hei ...

  9. EasyUI ComboBox ajax

    ajax请求数据 $.post("get_select", { "type1" : "mytype" }, function(data) { ...

  10. hihocoder周赛(树的最长距离)

    题目4 : 道路建设 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 H 国有 n 座城市和 n-1 条无向道路,保证每两座城市都可以通过道路互相到达.现在 H 国要开始 ...