当前位置:网站首页>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();
}
}
边栏推荐
- 2022/2/12 summary
- Mujoco finite state machine and trajectory tracking
- [2022 the finest in the whole network] how to test the interface test generally? Process and steps of interface test
- Advanced learning of MySQL -- basics -- basic operation of transactions
- Leecode brushes questions and records interview questions 01.02 Determine whether it is character rearrangement for each other
- Web project com mysql. cj. jdbc. Driver and com mysql. jdbc. Driver differences
- What can the interactive slide screen demonstration bring to the enterprise exhibition hall
- 【vulnhub】presidential1
- 【软件逆向-自动化】逆向工具大全
- Leecode brush questions record interview questions 32 - I. print binary tree from top to bottom
猜你喜欢
一图看懂对程序员的误解:西方程序员眼中的中国程序员
Alexnet experiment encounters: loss Nan, train ACC 0.100, test ACC 0.100
Distributed cache
Understand the misunderstanding of programmers: Chinese programmers in the eyes of Western programmers
48 page digital government smart government all in one solution
【vulnhub】presidential1
Mujoco Jacobi - inverse motion - sensor
2022年PMP项目管理考试敏捷知识点(9)
Geo data mining (III) enrichment analysis of go and KEGG using David database
rancher集成ldap,实现统一账号登录
随机推荐
Mujoco Jacobi - inverse motion - sensor
Win10 startup error, press F9 to enter how to repair?
What is web penetration testing_ Infiltration practice
37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
什么是时间
Three application characteristics of immersive projection in offline display
Amazon MemoryDB for Redis 和 Amazon ElastiCache for Redis 的内存优化
VTK volume rendering program design of 3D scanned volume data
Interesting wine culture
Imeta | Chen Chengjie / Xia Rui of South China Agricultural University released a simple method of constructing Circos map by tbtools
MySQL learning notes (mind map)
三维扫描体数据的VTK体绘制程序设计
2022/2/11 summary
Data sharing of the 835 postgraduate entrance examination of software engineering in Hainan University in 23
AI super clear repair resurfaces the light in Huang Jiaju's eyes, Lecun boss's "deep learning" course survival report, beautiful paintings only need one line of code, AI's latest paper | showmeai info
AI超清修复出黄家驹眼里的光、LeCun大佬《深度学习》课程生还报告、绝美画作只需一行代码、AI最新论文 | ShowMeAI资讯日报 #07.06
Rails 4 asset pipeline vendor asset images are not precompiled
JS+SVG爱心扩散动画js特效
Use type aliases in typescript