一、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. ldap搭建

    yum install openldap openldap-servers openldap-clients -y #检查是否安装成功 slapd -VVopenldap的配置文件都在/etc/ope ...

  2. Linux系统服务器 GNU Bash 环境变量远程命令执行漏洞修复命令

    具体方法就是在ssh上执行 yum update bash 完成后重启VPS.

  3. 清北学堂dp图论营游记day1

    讲课人: 老师对dp的理解是类似于分治思想,由小状态推出大状态.不同的是分治算法没有重叠子问题. dp把子问题越划越小,从而推出了基础状态.然后是dp方程,要满足简洁性,并且充分描述能够影响最后结果的 ...

  4. oracle order by 自定义

    我们通常需要根据客户需求对于查询出来的结果给客户提供自定义的排序方式,那么我们通常sql需要实现方式都有哪些,参考更多资料总结如下(不完善的和错误望大家指出): 一.如果我们只是对于在某个程序中的应用 ...

  5. 构建的Web应用界面不够好看?快试试最新的Kendo UI R3 2019

    通过70多个可自定义的UI组件,Kendo UI可以创建数据丰富的桌面.平板和移动Web应用程序.通过响应式的布局.强大的数据绑定.跨浏览器兼容性和即时使用的主题,Kendo UI将开发时间加快了50 ...

  6. Puppetnginx 架构图

    Puppetnginx 架构图 优点 *性能:nginx因为精简,运行起来非常快速,许多人声称它的比pound更高效.*日志,调试:在这两个方面,nginx比pound更简洁.*灵活性:nginx的处 ...

  7. hadoop namenode切换

    hdfs haadmin -transitionToActive --forcemanual nn1 将nn1强制转换为Active hdfs haadmin -transitionToStandby ...

  8. qt5--数据类型转换

    QString-->Char*        str.toUtf8().data() pointf=QPointF(point);       //将QPoint转换为QPointF point ...

  9. Mybatis中通过父类/接口来限定类的别名(TypeAlias)配置

  10. jquery pageY属性 语法

    jquery pageY属性 语法 作用:pageY() 属性是鼠标指针的位置,相对于文档的上边缘.直线模组 语法:event.page 参数: 参数 描述 event     必需.规定要使用的事件 ...