一、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. Delphi Memo组件

  2. centos 7 私有云盘 OwnCloud 安装搭建脚本

    #!/bin/bash #Build LAMP Server Conf mysql_secure_installation service mariadb restart systemctl enab ...

  3. 解决xshell小键盘输入时串码(干货!!)

    点击文件——属性 点击终端,修改为Linux即可

  4. Woobuntu

    Wooyun + Ubuntu = Woobuntu Woobuntu是基于Ubuntu系统的一款安全研究环境配置工具,可以自动安装并配置众多的安全工具与依赖环境,此外还针对中国用户的习惯进行了一些优 ...

  5. Could not determine which “make” command to run. Check the “make” step in the build configuration

    环境: QT5.10 VisualStudio2015 错误1: Could not determine which “make” command to run. Check the “make” s ...

  6. dedecms织梦移站后替换数据库中文件路径命令

    1.系统设置路径替换 update dede_sysconfig set value='http://afish.cnblogs.com' where varname='cfg_basehost'; ...

  7. Elasticsearch改动

    随着Elasticsearch的版本升级,Elasticsearch的一些特性也在改变,下面是一些需要注意的地方 v6.x 版本之前 : 一个index下面是可以创建多个type v6.x 版本 : ...

  8. fwrite()

    fwrite(),最好写strlen()个字节,否则可能有乱码

  9. vue admin element

    vue.package.js    修改  publicPath: './',

  10. 多线程(二)Object类方法、线程的操作sleep(),join(),interrupt(),yield()

    四.Object类简介 Object类是所有类的超类,之所以放在线程部分是因为其方法很多是和线程有关的.比如以下三个: wait()方法.wait(long timeout)和wait(long ti ...