当前位置:网站首页>接口中方法详解
接口中方法详解
2022-06-30 05:57:00 【Tmi】
一.接口简介
接口(interface),是java中一种引用数据类型,可以看做方法的集合,其内部主要就是封装了方法,包含抽象方法(JDK 7及以前),默认方法和静态方法(JDK 8),私有方法(JDK 9).
二.使用举例.
1.定义格式:
public interface 接口名称{
//抽象方法
//默认方法
//静态方法
//私有方法
}
2. 实现接口的类定义:
public class 类名 implements 接口名{
//实现接口中抽象方法(必须)
//重写接口中默认方法(可选)
}
3.注意:
- 接口不能创建对象,但可以被实现,接口的子类必须实现接口中所有的抽象方法.默认方法是可选的.
- 接口中无法定义变量,可以定义常量,默认使用public static final修饰(永远不可变
- 接口中没有构造方法,没有代码块
三.各类方法详解.
- 抽象方法:
使用abstract修饰,供子类实现使用
public abstract void method();
注意:所有方法在接口中(定义上)都是抽象方法,所以abstract关键字也可以不写,而接口代表对外行为的集合,在方法的声明中,所有都默认是 public,所以public也可以不写 可以直接写成(void methods);
默认方法
使用default修饰,不可省略,供子类调用或重写(只能通过最终实现类调用, 不能通过抽象类调用)
public default void method(){
//执行语句
}静态方法
用static 修饰,供接口直接调用.只能通过接口名称调用.子类不能继承或重写
public static void method(){
//执行语句
}私有方法
使用private修饰,供接口中的默认方法或静态方法调用
private void method(){
//执行语句
}
默认方法的使用举例:
例:(只能通过最终实现类(没有abstract修饰)调用,不能通过抽象类调用 )
public interface Test{
public default void print(){
System.out.print("我是默认方法");
}
}
public class TestImpl implements Test{
//1.什么也不写,通过对象即可调用
//2.或者进行重写
}
静态方法的使用:
例:(只能通过接口名称调用)
public interface Test{
public static void print(){
System.out.ptinr("我是静态方法");
}
}
public class TestImpl implements Test{
//无法重写也无法继承静态方法
}
边栏推荐
- 谁不想要一个自己的博客网站呢 - 搭建博客网站wordpress
- Force deduction exercise -- deleting repeated items in ordered sequence 1.0
- 10-【istio】istio Sidecar
- MySQL index
- MySQL advanced SQL statement
- On line assignment of financial cost management in the 22nd spring of Western Polytechnic University [Full Score answer]
- Online assignment of C language program design in the 22nd spring of Western Polytechnic University
- [exercise] basic practice letter graph of Blue Bridge Cup
- [Blue Bridge Road -- bug free code] DS1302 time module code analysis
- Cisco vxlan configuration
猜你喜欢
OSPF - authentication and load balancing summary (including configuration commands)
OpenCL线程代数库ViennaCL的使用
【板栗糖GIS】global mapper—如何把栅格的高程值赋予给点
Official win 10 image download
重构之美:当多线程批处理任务挑起大梁 - 万能脚手架
ECS deployment web project
leetcode763. Divide letter interval
Sound network, standing in the "soil" of the Internet of things
剑指 Offer 18. 删除链表的节点
Today, Ali came out with 35K. It's really sandpaper that wiped my ass. it showed me my hand
随机推荐
Xijiao 21 autumn "motor and drive" online homework answer sheet (III) [standard answer]
8 ways to earn passive income
SSL证书续费相关问题详解
What do you think of the deleted chat records? How to restore the deleted chat records on wechat?
Promise knowledge points
What kind of answer has Inspur given in the big AI model landing test?
Video summary of my station B
Use of tornado template
[exercise] basic practice letter graph of Blue Bridge Cup
leetcode763. Divide letter interval
云服务器部署 Web 项目
C language code record
After getting these performance test decomposition operations, your test path will be more smooth
MySQL數據庫用戶管理
Sound network, standing in the "soil" of the Internet of things
10-【istio】istio Sidecar
InputStream转InputStreamSource
声网,站在物联网的“土壤”里
Finally someone can make the server so straightforward
[OSPF] comparison between rip and OSPF