Interface default method介绍
一、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介绍的更多相关文章
- 深入学习Java8 Lambda (default method, lambda, function reference, java.util.function 包)
Java 8 Lambda .MethodReference.function包 多年前,学校讲述C#时,就已经知道有Lambda,也惊喜于它的方便,将函数式编程方式和面向对象式编程基于一身.此外在使 ...
- 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 ...
- jvm Classload method介绍
1,jvm Classload默认几个重要方法介绍 findClass:Finds and loads the class with the specified name from the URL s ...
- interface中定义default方法和static方法
interface的default方法和static方法 接口中可以定义static方法,可通过接口名称.方法名()调用,实现类不能继承static方法: 接口中可以定义default方法,defau ...
- java 小记
1.获取web项目根目录的绝对路径 request.getContextPath() 获取项目名称,如 /BiYeSheJi getServletContext().getRealPath(& ...
- Java 8新特性——default方法(defender方法)介绍
我们都知道在Java语言的接口中只能定义方法名,而不能包含方法的具体实现代码.接口中定义的方法必须在接口的非抽象子类中实现.下面就是关于接口的一个例子: 1 2 3 4 5 6 7 8 9 10 11 ...
- 关于java8 interface的default方法
转自鸟窝 博主写的挺详细,不了解的看一看啊 以前经常谈论的Java对比c++的一个优势是Java中没有多继承的问题. 因为Java中子类只能继承(extends)单个父类, 尽管可以实现(implem ...
- 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 ...
- Getting start with dbus in systemd (01) - Interface, method, path
Getting start with dbus in systemd (01) 基本概念 几个概念 dbus name: connetion: 如下,第一行,看到的就是 "dbus name ...
随机推荐
- json文件处理四个函数
import json # json.dumps(json_dict,ensure_asscii = False)函数的使用,将字典转化为字符串 ensure_ascii=False将Unicode编 ...
- linux工具之pmap
1.pmap简介 pmap命令用来报告一个进程或多个进程的内存映射.可以使用这个工具确定系统是如何为服务器上的进程分配内存的. 例如查看ssh进程的内存映射:
- MVC中 global.asax
MVC框架下 global.asax 页面的事件 这些事件被触发的 顺序是: Application_BeginRequest Application_AuthenticateRequest Appl ...
- 杜教BM模板
#include<bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #defi ...
- FushionCharts
FushionCharts官网:http://www.fusioncharts.com/ 在线Demo:http://www.fusioncharts.com/free/demos/Blueprint ...
- [易学易懂系列|rustlang语言|零基础|快速入门|(19)|多线程]
[易学易懂系列|rustlang语言|零基础|快速入门|(19)|多线程] 实用知识 多线程 我们今天来讲讲Rust中的多线程. 我直接来看看代码: use std::thread; use std: ...
- mysql5.7.26 基于GTID的主从复制环境搭建
简单工作原理: (1)从库执行 change master to 语句,会立即将主库信息记录到master.info中 (2)从库执行 start slave语句,会立即生成IO_T和SQL_T (3 ...
- 【CF1181D】Irrigation
题目大意:给定 M 个城市,每年会选出一个城市举办比赛,现给出前 N 年城市举办比赛的情况.在接下来的年份中,每年会在举办比赛次数最小的城市举办比赛,如果有很多城市举办次数均为最小值,则在编号最小的城 ...
- 使用Vue做个简单的评论 + localstorage存储
1.引入Vue.js 2.编写代码 代码 <!DOCTYPE html> <html lang="zh"> <head> <meta c ...
- iOS-修改TableView分割线样式
实现代码: myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 有三种样式: UITableViewCellS ...