Java8新特性——接口中的默认方法和静态方法

news/2024/7/22 15:39:49

1.接口中的默认方法和静态方法

Java 8中允许接口中包含具有具体实现的方法,该方法称为 “默认方法” ,默认方法使用 default 关键字修饰。

接口默认方法的 ” 类优先 ” 原则。若一个接口中定义了一个默认方法,而另外一个父类或接口中又定义了一个同名的方法时

  • 选择父类中的方法。如果一个父类提供了具体的实现,那么接口中具有相同名称和参数的默认方法会被忽略。
  • 接口冲突。如果一个父接口提供一个默认方法,而另一个接口也提供了一个具有相同名称和参数列表的方法(不管方法是否是默认方法),那么必须覆盖该方法来解决冲突。

同时,Java8 中,允许接口中添加静态方法(static)。


2.应用举例

package com.szh.java8;

/**
 *
 */
interface InterfaceOne {

    default String getMessage() {
        return "哈哈哈";
    }

    static void show() {
        System.out.println("接口中的静态方法");
    }
}

interface InterfaceTwo {
    default String getMessage() {
        return "嘿嘿嘿";
    }
}

class ClassOne {
    public String getMessage() {
        return "呵呵呵";
    }
}

class SubClass1 extends ClassOne implements InterfaceOne {

}

class SubClass2 extends ClassOne implements InterfaceOne,InterfaceTwo {

}

class SubClass3 implements InterfaceOne,InterfaceTwo {

    @Override
    public String getMessage() {
        return InterfaceTwo.super.getMessage();
    }

}

public class TestClass {

    public static void main(String[] args) {
        SubClass1 sub1 = new SubClass1();
        System.out.println(sub1.getMessage());

        SubClass2 sub2 = new SubClass2();
        System.out.println(sub2.getMessage());

        SubClass3 sub3 = new SubClass3();
        System.out.println(sub3.getMessage());

        InterfaceOne.show();
    }
}

虽然SubClass1类继承了ClassOne、同时实现了InterfaceOne,由于类优先原则,所以这里执行的是ClassOne类中的getMessage方法。

SubClass2与SubClass1是同样的道理。

而SubClass3就不一样了,它没有继承ClassOne这个类,而是同时实现了InterfaceOne、InterfaceTwo这两个接口,那么它就必须选择其中一个来对默认方法进行实现(覆盖 / 重写),实现的是哪个接口,相应的就执行哪个接口的默认方法。

最后接口中的静态方法就和类中的静态方法一样,直接 接口名.静态方法名 就可以调用了。


http://www.niftyadmin.cn/n/711140.html

相关文章

日志信息log

#include<syslog.h> //建立一个到系统日志的连接 //ident参数指向字符串&#xff0c;syslog()输出的每条信息都会包含这个字符串&#xff0c;这个参数的取值通常是程序名 //log_options参数是一个位掩码 //LOG_CONS 当向系统日志发送信息发生错误时将信息写入到系统控制…

mmm hardware/libhardware_legacy/power/

android源码目录下的build/envsetup.sh文件&#xff0c;描述编译的命令 - m: Makes from the top of the tree. - mm: Builds all of the modules in the current directory. - mmm: Builds all of the modules in the supplied directories. 要想使用这些命…

关于form表单回车自动刷新

现象&#xff1a; form表单&#xff0c;输入框聚焦后&#xff0c;回车&#xff0c;页面刷新跳转。 原因&#xff1a; form表单&#xff0c;在只有一个输入框的时候&#xff0c;在点击回车时&#xff0c;就会触发表单的提交&#xff0c;而form若没有url&#xff0c;则提交后就会…

Java8新特性——新一套日期时间API

文章目录&#xff1a; 1.新旧对比&#xff08;线程安全问题&#xff09; 2.LocalDate 3.LocalTime 4.LocalDateTime 5.Instant 6.Duration、Period 7.TestTemporalAdjuster、TestTemporalAdjusters 8.DateTimeFormatter 1.新旧对比&#xff08;线程安全问题&#xff09…

Request.InputStream 将数据作为XML数据发送

将数据作为XML数据发送,例如: public voidPostXml(stringurl, stringxml){ byte[] bytes Encoding.UTF8.GetBytes(xml); HttpWebRequest request (HttpWebRequest) WebRequest.Create(url); request.Method "POST"; request.ContentLength bytes.Len…

oauth2学习

oauth2 生词&#xff1a; 授权码模式&#xff08;authorization code&#xff09;简化模式&#xff08;implicit&#xff09;密码模式&#xff08;resource owner password credentials&#xff09;客户端模式&#xff08;client credentials&#xff09;问题&#xff1a; 分享…

AndroidImagePicker 的使用

github地址 https://github.com/easonline/AndroidImagePicker 效果图 本文转自Work Hard Work Smart博客园博客&#xff0c;原文链接&#xff1a;http://www.cnblogs.com/linlf03/p/5235541.html&#xff0c;如需转载请自行联系原作者

SpringBoot——四大核心之指标监控(actuator)

1.写在前面 首先肯定要说一下SpringBoot的四大核心了&#xff1a; 自动装配&#xff1a;简单配置甚至零配置即可运行项目起步依赖&#xff1a;场景启动器Actuator&#xff1a;指标监控命令行界面 &#xff1a;命令行这篇文章呢&#xff0c;我来和大家聊聊指标监控这个东西。 2.…