当前位置:网站首页>接口中方法详解
接口中方法详解
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{
//无法重写也无法继承静态方法
}

边栏推荐
- 从零开发 stylelint规则(插件)
- Detailed explanation of issues related to SSL certificate renewal
- inno setup 最简单的自定义界面效果
- The definition of strain was originally from stretch_ Ratio started
- MySQL advanced (Advanced SQL statement)
- Is it safe to open an account and trade with a compass?
- Did you know that WPS can turn on eye protection mode?
- VLAN access mode
- Leader: who can use redis expired monitoring to close orders and get out of here!
- At the age of 32, I fell into a middle-aged crisis and finally quit naked...
猜你喜欢

Leetcode56. consolidation interval
![[MD editing required] welcome to the CSDN markdown editor](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[MD editing required] welcome to the CSDN markdown editor

剑指 Offer 22. 链表中倒数第k个节点

MySQL advanced (Advanced SQL statement)

At the age of 32, I fell into a middle-aged crisis and finally quit naked...

【LeetCode】236. Nearest common ancestor of binary tree

C语言基础小操作

After getting these performance test decomposition operations, your test path will be more smooth

Do you know how to show the health code in only 2 steps

Sound net, debout dans le "sol" de l'IOT
随机推荐
谁不想要一个自己的博客网站呢 - 搭建博客网站wordpress
Is it safe to open an account and trade with a compass?
Qt之QListView的简单使用(含源码+注释)
Sword finger offer 18 Delete the node of the linked list
09- [istio] istio service entry
Did you know that WPS can turn on eye protection mode?
What do you think of the deleted chat records? How to restore the deleted chat records on wechat?
Lantern Festival | maoqiu technology and everyone "guess riddles and have a good night"
Sword finger offer 29 Print matrix clockwise
Force deduction exercise -- deleting repeated items in ordered sequence 1.0
Related applications of priority queue
C language code record
[MD editing required] welcome to the CSDN markdown editor
MySQL事物
飞升:基于中文分词器IK-2种自定义热词分词器构建方式showcase & 排坑showtime
Stack overflow caused by C # using protobuf stack overflow
【数据库】事务
How to write a thesis
Finally someone can make the server so straightforward
After getting these performance test decomposition operations, your test path will be more smooth