当前位置:网站首页>Interface (interface related meaning, different abstract classes, interface callback)
Interface (interface related meaning, different abstract classes, interface callback)
2022-07-07 00:42:00 【A very lazy person】
This paper introduces the related knowledge of interface in detail , Including but not limited to the following knowledge :
- The concept of interface 、 grammar 、 Use of interfaces 、 Commonly used way
- The difference between an interface and an abstract class ( Rule of grammar 、 Qualitative distinction )
- Interface callback ( Deeply understand the idea of interface callback , Case code is attached )
The interface is super detailed
1. Interface
(1) Concept
Macroscopic
The rules
Microcosmic
function
summary :
Interfaces can be seen as java An abstraction of behavior .
(2) Grammar definition
grammar :
Access modifier interface The interface name {
}
(3) What is in the interface
**1、2 This is very important **
1, Public static constants
The properties in the interface use... By default public static final modification
2, Interfaces can have public abstract methods
The abstract methods in the interface use public abstract modification
3, Public static methods , Cannot be inherited
4, Interfaces can have default A common way of decorating ( understand )
5, Interface does not have a constructor
6, There are no code blocks in the interface
(4) Use of interfaces : Inheritance and implementation
Interface and interface : Multiple inheritance
keyword :extends
grammar :
Access modifier interface The interface name extends Parent interface name 1, Parent interface name 2,...{
}
Be careful :
1, Static methods that do not inherit the parent interface
2, You can inherit the public static constants of the parent interface
3, You can inherit all abstract methods of the parent interface
Interfaces and classes : Multiple implementation
keyword :implements
grammar :
Access modifier class Class name extends Parent class name implements Parent interface name 1, Parent interface name 2,...{
}
Be careful :
1, A class can implement multiple interfaces
2, Or this class overrides the abstract methods of all interfaces , Or you are an abstract class
(5) Commonly used way
1, constant interface
All defined in the interface are constants
reason : The properties in the interface use... By default public static final modification
eg: You need to record several values that will never change
public class Utils{
public static final int ERROR_01 = 500;
public static final int ERROR_02 = 404;
public static final int ERROR_03 = 305;
public static final int OK = 200;
}
2, Empty interface
There is nothing in the interface
reason : Represents a rule , So that its subclass object can be converted to the interface object
3, Interface callback
See below !
2. The difference between an interface and an abstract class
(1) Grammatical rules differ
same
1, You can't create objects directly
2, Can be compiled into class file
Different
1, There are no common attributes in the interface , Abstract classes have
2, There are no code blocks and static code blocks in the interface , Abstract classes have
3, There is no constructor in the interface , Abstract classes have
4, There are no ordinary methods in the interface , Abstract classes have
5, Interfaces can only be implemented by classes , Abstract classes can only be inherited , A class can only inherit one class , But you can implement multiple interfaces
6, The essence of an interface is a rule or function , The essence of an abstract class is a concept
(2)java Qualitative difference
1, An abstract class is an abstraction of something , That is, to abstract the class , And interfaces are abstractions of behavior .
Abstract class is to abstract the whole class , Including attributes 、 Behavior , But the interface is local to the class ( Behavior ) Abstraction .
2, Different levels of design , The abstract class ACTS as the parent of many subclasses , It is a kind of Template design .
An interface is a behavior specification , It is a kind of Radial design .
3, An abstract class is a class , Interfaces are not classes .
3. Interface callback
(1) Interface variables
- Interfaces are the same as classes , yes Java A data type in , Variables declared with interfaces are called interface variables
- Stored in the interface variable is Realized The reference of the instance of the class of this interface
(2) Interface callback concept
Interface callback Borrowed C In language Pointer callback The term of
Pointer callback , Indicates that the address of a variable is stored in a Pointer to the variable in , This Pointer to the variable The data stored in this variable can be operated indirectly ;
Interface callback , It means that the reference of the object created by the class that implements an interface can be assigned to the interface variable declared by the interface , Then the Interface variables can call the interface methods implemented by the class .
- ( In fact, when Interface variables When calling the interface method implemented by the class , Is to notify the corresponding object to call this method )
- But interface variables cannot call other non interface methods in the class ;( It is similar to the method overridden by subclasses called by upper transformation objects )
(3) Interface callback and polymorphism
Reference the instance of the class that implements the interface Assign values to interface variables after , This interface variable can Callback Class overrides interface methods .
Different classes may have different implementation methods when implementing the same interface , Then interface variables may have various forms when calling back interface methods .
(4) Interface parameters
If the parameter of a method is interface type data , Then you can pass the reference of the instance of any class that implements the interface to the parameter , Interface parameters can also call back methods implemented by classes
(5) General steps of interface callback
step :
- stay A Create interface objects in
- stay A establish B object
- Use B Object call method , Pass the interface object to B in
- stay A Use in B Object call method , This method uses the interface object to call the interface method
Interface callback implementation case : Button click
analysis :
Users clicking different buttons will trigger different events
for example :
Click the keyboard when playing games w、a、s、d Will trigger different events , Release different skills , therefore w、a、s、d All have the behavior of releasing skills ( function )
This function can then be encapsulated into the interface , For buttons, they must realize this function
( Click to release skills , Different buttons have different effects , You can use the interface callback to realize the preset click effect of different buttons )
------1. Interface ( Abstract a behavioral feature )------
public interface OnClickLisenter {
public void onClick();
}
------2. Interface implementation class ( Button click event : Clicking the button will trigger this event ( Behavior ))( Different buttons have different implementation methods )------
public class EventBtn01 implements OnClickLisenter {
@Override
public void onClick() {
// TODO Auto-generated method stub
System.out.println(" Hello , The world !");
}
}
public class EventBtn02 implements OnClickLisenter{
@Override
public void onClick() {
// TODO Auto-generated method stub
System.out.println(" Hello ,JAVA");
}
}
------3. Button class ( Receive implementation classes as interface variables , It can shield the difference of events , Achieve different effects of clicking )------
public class Button {
// Accept the events passed in the form of interface variables , namely : Shield the differences of events
private OnClickLisenter onClickLisenter;
// Monitor click events ( Formal parameters are received in the form of interface variables )
public void setOnClickLisenter(OnClickLisenter onClickLisenter) {
this.onClickLisenter = onClickLisenter;
}
// Click the button to trigger the event
public void down() {
onClickLisenter.onClick();
}
}
------4. Test environment ------
/* * Click button ( Button 1 Pop up Hello world , Button 2 Pop up good programmers ) * * ( There is a click function , Two click function effects can be abstracted as events ( Different event incoming buttons , The click effect is different )) * */
public class Test {
public static void main(String[] args) {
// The new button
Button button=new Button();
// New event
EventBtn01 event01=new EventBtn01();
EventBtn02 event02=new EventBtn02();
// Set listening Events ( The event triggered by clicking the button )
button.setOnClickLisenter(event01);
// Click button
button.down();
}
}
边栏推荐
- rancher集成ldap,实现统一账号登录
- Hero League | King | cross the line of fire BGM AI score competition sharing
- 2022/2/12 summary
- 5种不同的代码相似性检测,以及代码相似性检测的发展趋势
- Things like random
- Web project com mysql. cj. jdbc. Driver and com mysql. jdbc. Driver differences
- If the college entrance examination goes well, I'm already graying out at the construction site at the moment
- How can computers ensure data security in the quantum era? The United States announced four alternative encryption algorithms
- Value Function Approximation
- 互动滑轨屏演示能为企业展厅带来什么
猜你喜欢
Are you ready to automate continuous deployment in ci/cd?
Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23
Imeta | Chen Chengjie / Xia Rui of South China Agricultural University released a simple method of constructing Circos map by tbtools
深度学习之环境配置 jupyter notebook
【vulnhub】presidential1
Attention SLAM:一种从人类注意中学习的视觉单目SLAM
stm32F407-------SPI通信
[2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
Amazon MemoryDB for Redis 和 Amazon ElastiCache for Redis 的内存优化
Geo data mining (III) enrichment analysis of go and KEGG using David database
随机推荐
C语言输入/输出流和文件操作【二】
The difference between redirectto and navigateto in uniapp
uniapp中redirectTo和navigateTo的区别
How engineers treat open source -- the heartfelt words of an old engineer
Leecode brush questions record sword finger offer 11 Rotate the minimum number of the array
37頁數字鄉村振興智慧農業整體規劃建設方案
How to set encoding in idea
Markov decision process
Use mujoco to simulate Cassie robot
[vector retrieval research series] product introduction
VTK volume rendering program design of 3D scanned volume data
Web project com mysql. cj. jdbc. Driver and com mysql. jdbc. Driver differences
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
Stm32f407 ------- DAC digital to analog conversion
【软件逆向-自动化】逆向工具大全
Use type aliases in typescript
[yolov5 6.0 | 6.1 deploy tensorrt to torch serve] environment construction | model transformation | engine model deployment (detailed packet file writing method)
2021 SASE integration strategic roadmap (I)
Article management system based on SSM framework
Leecode brush question record sword finger offer 56 - ii Number of occurrences of numbers in the array II