当前位置:网站首页>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/10 summary
- threejs图片变形放大全屏动画js特效
- Three methods to realize JS asynchronous loading
- Notes of training courses selected by Massey school
- 37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
- ZYNQ移植uCOSIII
- Model-Free Prediction
- Rails 4 asset pipeline vendor asset images are not precompiled
- Racher integrates LDAP to realize unified account login
- Quaternion attitude calculation of madgwick
猜你喜欢
Core knowledge of distributed cache
File and image comparison tool kaleidoscope latest download
深度学习之环境配置 jupyter notebook
一图看懂对程序员的误解:西方程序员眼中的中国程序员
Distributed cache
Zynq transplant ucosiii
equals()与hashCode()
Uniapp uploads and displays avatars locally, and converts avatars into Base64 format and stores them in MySQL database
uniapp中redirectTo和navigateTo的区别
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
随机推荐
C9高校,博士生一作发Nature!
Basic information of mujoco
Notes of training courses selected by Massey school
互动滑轨屏演示能为企业展厅带来什么
37 page overall planning and construction plan for digital Village revitalization of smart agriculture
集合(泛型 & List & Set & 自定义排序)
dynamic programming
浅谈测试开发怎么入门,如何提升?
Leecode brush questions record sword finger offer 43 The number of occurrences of 1 in integers 1 to n
File and image comparison tool kaleidoscope latest download
Advanced learning of MySQL -- basics -- multi table query -- external connection
Devops can help reduce technology debt in ten ways
Google, Baidu and Yahoo are general search engines developed by Chinese companies_ Baidu search engine URL
Cross-entrpy Method
学习光线跟踪一样的自3D表征Ego3RT
X.509 certificate based on go language
37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
How engineers treat open source -- the heartfelt words of an old engineer
Compilation of kickstart file