当前位置:网站首页>Detailed description of methods in the interface
Detailed description of methods in the interface
2022-06-30 06:06:00 【Tmi】
List of articles
One . Interface profile
Interface (interface), yes java A reference data type in , It can be seen as a collection of methods , Its interior mainly encapsulates the methods , Contains abstract methods (JDK 7 And before ), Default and static methods (JDK 8), Private method (JDK 9).
Two . Use examples .
1. Define format :
public interface The name of the interface {
// Abstract method
// The default method
// Static methods
// Private method
}
2. Class definition of implementation interface :
public class Class name implements The interface name {
// Implement abstract methods in the interface ( must )
// Override the default method in the interface ( Optional )
}
3. Be careful :
- Interface cannot create object , But it can be realized , Subclasses of an interface must implement all abstract methods in the interface . The default method is optional .
- Variable cannot be defined in interface , You can define constants , By default public static final modification ( Forever immutable
- There is no constructor in the interface , No code blocks
3、 ... and . Various methods are explained in detail .
- Abstract method :
Use abstract modification , For subclass implementation
public abstract void method();
Be careful : All methods are in the interface ( By definition ) It's all abstract methods , therefore abstract Keywords can be left blank , Interfaces represent a collection of external behaviors , In the declaration of the method , All defaults to public, therefore public Or not It can be written directly as (void methods);
The default method
Use default modification , Do not omit , For subclasses to call or override ( Can only be called through the final implementation class , Cannot call... Through an abstract class )
public default void method(){
// Execute statement
}Static methods
use static modification , For the interface to call directly . Can only be called by interface name . Subclasses cannot inherit or override
public static void method(){
// Execute statement
}Private method
Use private modification , For the default method or static method in the interface
private void method(){
// Execute statement
}
Examples of using default methods :
example :( Only through the final implementation class ( No, abstract modification ) call , Cannot call... Through an abstract class )
public interface Test{
public default void print(){
System.out.print(" I'm the default method ");
}
}
public class TestImpl implements Test{
//1. Write nothing , You can call... Through an object
//2. Or rewrite
}
Use of static methods :
example :( Can only be called by interface name )
public interface Test{
public static void print(){
System.out.ptinr(" I'm a static method ");
}
}
public class TestImpl implements Test{
// Cannot override or inherit static methods
}

边栏推荐
- Who doesn't want a blog site of their own - build a blog site WordPress
- MySQL summary
- Verilog中case,casez,casex语句的用法
- Use of tornado template
- ES6数组遍历与ES5数组遍历
- MySQL數據庫用戶管理
- Decompilation normal decompilation problems. Solve them yourself
- Create uiactionsheet [duplicate] - creating uiactionsheet [duplicate]
- MySQL高级SQL语句
- MySQL advanced SQL statement
猜你喜欢

Verilog中case,casez,casex语句的用法

MySQL日志管理、数据备份、恢复

Swoole process model diagram
![09- [istio] istio service entry](/img/48/86f8ec916201eefc6ca09c45a60a6a.jpg)
09- [istio] istio service entry

多线程进阶篇

IP TCP UDP network encryption suite format

Xctf attack and defense world crypto advanced area

JS prototype chain object function relationship

Dynamic programming -- gliding wing of the strange thief Kidd

MySQL transaction
随机推荐
Codeforces B. MEX and Array
网络基础知识
文件操作IO-Part1
InputStream to inputstreamsource
Finally someone can make the server so straightforward
OSPF - authentication and load balancing summary (including configuration commands)
超简单 STM32 RTC闹钟 时钟配置
Swoole process model diagram
关于Glide加载图片模糊不清楚
Go pack and unpack
[regular expression series] greedy and non greedy patterns
电脑查看WiFi使用密码
MySQL日志管理、数据备份、恢复
Leetcode56. consolidation interval
Cisco vxlan configuration
Beauty of Refactoring: when multithreaded batch processing task lifts the beam - Universal scaffold
云服务器部署 Web 项目
MySQL log management, data backup and recovery
观察者模式、状态模式在实际工作中的使用
583. 两个字符串的删除操作-动态规划