论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
朋友圈
看朋友圈动态,了解ToB世界。
ToB门户
了解全球最新的ToB事件
博客
Blog
排行榜
Ranklist
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
导读
Guide
相册
Album
记录
Doing
搜索
本版
文章
帖子
ToB圈子
用户
免费入驻
产品入驻
解决方案入驻
公司入驻
案例入驻
登录
·
注册
只需一步,快速开始
账号登录
立即注册
找回密码
用户名
Email
自动登录
找回密码
密码
登录
立即注册
首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
圈子
SAAS
ToB企服应用市场:ToB评测及商务社交产业平台
»
论坛
›
物联网
›
物联网
›
JDK7 JDK8 JDK9接口中的默认方法、静态方法、私有方法 ...
JDK7 JDK8 JDK9接口中的默认方法、静态方法、私有方法
不到断气不罢休
金牌会员
|
2024-6-15 01:59:21
|
显示全部楼层
|
阅读模式
楼主
主题
543
|
帖子
543
|
积分
1629
JDK8开始之后接口新增的方法
JDK7从前:接口中只能定义抽象方法
JDK8的新特性:接口中可以定义有方法体的方法(默认、静态)
JDK9的新特性:接口中可以定义私有方法
<strong> 接口中的默认方法
</strong>
InterA
复制代码
package com.itheima.a06;
public interface InterA {
// 抽象方法 不能拥有方法体
public abstract void show();
// public 可以省略 default不可以省略
public default void print() {
System.out.println("A接口中的默认方法---print");
}
}
复制代码
InterB
复制代码
package com.itheima.a06;
public interface InterB {
// 抽象方法 不能拥有方法体
public abstract void show();
// public 可以省略 default不可以省略
public default void print() {
System.out.println("B接口中的默认方法---print");
}
}
复制代码
intermpl
复制代码
package com.itheima.a06;
public class intermpl implements InterA,InterB {
@Override
public void show() {
System.out.println("实现类 重写的抽象方法");
}
// 默认方法不是抽象方法 不强制重写 如果重写 去掉default关键字
// 实现多个接口 多个接口存在相同名字的默认方法,子类就必须对该方法重写
@Override
public void print() {
System.out.println("重写接口中的默认方法");
}
}
复制代码
Test
复制代码
package com.itheima.a06;
public class Test {
public static void main(String[] args) {
intermpl ii=new intermpl();
ii.show();
ii.print();
}
}
复制代码
JDK8以后新增的静态方法
Inter
复制代码
package com.itheima.a07;
public interface Inter {
public abstract void method();
// 静态方法不能被重写
public static void show(){
System.out.println("Inter方法中的静态方法");
};
}
复制代码
InterImpl
复制代码
package com.itheima.a07;
public class InterImpl implements Inter {
@Override
public void method() {
System.out.println("InterImpl中重写的抽象方法");
}
// 不叫重写
public static void show(){
System.out.println("InterImpl方法中的静态方法");
};
}
复制代码
Test
复制代码
package com.itheima.a07;
public class Test {
public static void main(String[] args) {
// 调用接口中的静态方法
Inter.show();
// 调用实现类中的静态方法
InterImpl.show();
}
}
复制代码
JDK9以后的私有方法
InterA
复制代码
package com.itheima.a08;
public interface InterA {
public default void show1(){
System.out.println("show1方法执行");
show3();
};
public default void show2(){
System.out.println("show2方法执行");
show3();
};
//普通私有方法,给默认方法服务
private void show3(){
System.out.println("记录细节的代码");
};
public static void show11(){
System.out.println("show1方法执行");
show4();
};
public static void show22(){
System.out.println("show2方法执行");
show4();
};
//静态私有方法,给静态方法服务
private static void show4(){
System.out.println("记录细节的代码");
};
}
复制代码
总结
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
本帖子中包含更多资源
您需要
登录
才可以下载或查看,没有账号?
立即注册
x
回复
使用道具
举报
0 个回复
正序浏览
返回列表
快速回复
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
or
立即注册
本版积分规则
发表回复
回帖并转播
发新帖
回复
不到断气不罢休
金牌会员
这个人很懒什么都没写!
楼主热帖
Spark的一些重要概念
物联网无线数传通信模块设备常见的几种 ...
手绘地图深度解析:类型、风格、功能、 ...
第一部分:介绍 Spdlog 日志库 ...
从SAP ECC升级到SAP S4HANA, 几个Key P ...
p2 IO流原理及流的分类
php解决缓存击穿的问题
微服务组件-----Spring Cloud Alibaba ...
什么是计算机网络
【Redis高手修炼之路】初学Redis——概 ...
标签云
挺好的
服务器
快速回复
返回顶部
返回列表