当前位置:网站首页>Quick learning 1.8 front and rear interfaces
Quick learning 1.8 front and rear interfaces
2022-07-03 12:45:00 【Persia_ Pepper】
Basic concept of quick connect interface
JDK1.8 Before
1. Interface declaration format
[ Access modifier ] interface The interface name [extends The parent interface 1, The parent interface 2…] {
Constant definition ;
Method definition ;
}
2. Code
/* 1. Class is a class , An interface is an interface , They are concepts at the same level 2. There is no constructor in the interface 3. How is an interface declared : interface 4. stay JDK1.8 Before , The interface has only two parts (1) Constant :public static final (2) Abstract method : public abstract Be careful : Modifiers can be omitted without writing ,IDE It will be completed by default , But beginners suggest writing , Increase impression */
public interface TestInterface {
// Constant
/*public static final*/ int NUM = 10;
// Abstract method
/*public abstract*/ void a();
/*public abstract*/ void b(int num);
/*public abstract*/ int c(String name);
}
interface TestInterface02{
void e();
void f();
}
/* 5. What is the relationship between classes and interfaces ? Realization relationship : Class implementation interface 6. Once an interface is implemented , Then the implementation class should override all abstract methods of the interface 7. If you don't rewrite all abstract methods , Then this class can become an abstract class 8.java Only a single inheritance , however java How to achieve One class inherits other classes , But it can only inherit one parent class But if the implementation class implements the interface , Multiple interfaces can be implemented 9. How to write it : Inherit first Re realization : extends Person implements TestInterface,TestInterface02 */
class Student extends Person implements TestInterface,TestInterface02{
@Override
public void a() {
System.out.println("------1");
}
@Override
public void b(int num) {
System.out.println("------2"+num);
}
@Override
public int c(String name) {
return 10;
}
@Override
public void e() {
System.out.println("------3");
}
@Override
public void f() {
System.out.println("------4");
}
}
class Test{
public static void main(String[] args) {
// Interface cannot be instantiated , That is, you can't create objects
// TestInterface t = new TestInterface();
TestInterface02 t = new Student(); // Interface to implementation class ---》 polymorphic
//11. How constants in the interface are accessed
System.out.println(TestInterface.NUM);
System.out.println(Student.NUM);
Student s = new Student();
System.out.println(s.NUM);
TestInterface t2 = new Student();
System.out.println(t2.NUM);
}
}
3. Function of interface
Interface is mainly used to define rules , The implementation class is responsible for the implementation ;
The difference from abstract classes is , It is an interface, not a class
4. Conceptual analysis
Inherit : Subclass inherits from parent class
Realization : The implementation class implements the interface
Explain with examples :
“ mobile phone ” Is it right? “ camera ”
Inherit : mobile phone extends camera 'is-a’ The relationship between , A mobile phone is a camera
The above expression , I can feel that it is not good enough
Realization : mobile phone implement camera 'has-a’ The relationship between , The mobile phone has the function of camera
Case study : The plane , Little bird , Kite
Define an interface :Flyable【 They can all realize the function of flying , But obviously they can't be grouped , Say they inherited from a flying class , Obviously not , This has to be understood slowly in practice 】
5. Polymorphic applications
(1) The parent class is used as the formal parameter of the method , Objects passing in specific subclasses
(2) The parent class is used as the return value of the method , Return objects of specific subclasses
(3) Interfaces are used as formal parameters of methods , Pass in the object of the concrete implementation class
(4) Interface as the return value of a method , Return the object of the concrete implementation class
6. The difference between interface and abstract class
Intercept Baidu's brief description , You can briefly understand , Because now most of them use 1.8 Later versions , There will be some differences
JDK1.8 Then add non abstract methods
Take a look back. JDK1.8 Before , Interface only has two parts
1. Constant : Fixed modifier :public static final
2. Abstract method : Fixed modifier :public abstract
By public default Non abstract methods of modification
- default The modifier must be added, otherwise an error will be reported
- If the implementation class wants to rewrite the non abstract methods in the interface , that default Must not add , Otherwise, the report will be wrong
public interface TestInterface18 {
//1.8 Interface after
// Constant
public static final int NUM = 10;
// Abstract method
public abstract void a();
//public default Non abstract methods of modification
public default void b(){
System.out.println("----public default,method---");
}
}
class A implements TestInterface18 {
public void c(){
// Use the interface b Method
b();// Sure
//super.b();// Can not be , Interface cannot be regarded as the parent of this class
TestInterface18.super.b();// Sure , Special interface method calling mode
}
@Override
public void a() {
System.out.println(" Interface rewritten a Abstract methods in ");
}
@Override
public void b() {
}
}
Static methods
- static Don't omit not to write
- Static methods cannot override
public interface TestInterface3 {
// Constant :
public static final int NUM = 10;
// Abstract method :
public abstract void a();
//public default Nonabstract method ;
public default void b(){
System.out.println("-----TestInterface3---b");
}
// Static methods
public static void c(){
System.out.println("-----TestInterface3 Static methods ");
}
}
class Demo implements TestInterface3{
@Override
public void a() {
System.out.println(" Rewrote a Method ");
}
public static void c(){
System.out.println("Demo Static methods in ");
}
}
class C{
public static void main(String[] args) {
Demo d = new Demo();
d.c();
Demo.c();
TestInterface3.c();
}
}
Running results :
Demo Static methods in
Demo Static methods in
-----TestInterface3 Static methods
Why should we add non abstract methods to the interface ?
If only abstract methods can be added to the interface , When changing the interface , The impact on implementation classes is too great , All implementation classes should be rewritten .
Now add non abstract methods to the interface , It has little impact on the implementation class , Just call... If you want .
边栏推荐
- 【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
- Togaf certification self-study classic v2.0
- Do you feel like you've learned something and forgotten it?
- 2.7 overview of livedata knowledge points
- Nodejs+express+mysql realizes login function (including verification code)
- I'm too lazy to write more than one character
- 02_ Lock the code, and don't let the "lock" become a worry
- 基于Linu开发的项目视频
- Apache Mina开发手册
- [download attached] password acquisition tool lazagne installation and use
猜你喜欢
2.8 overview of ViewModel knowledge
最新版盲盒商城thinkphp+uniapp
电压环对 PFC 系统性能影响分析
initial、inherit、unset、revert和all的区别
记录自己vulnhub闯关记录
Use Tencent cloud IOT platform to connect custom esp8266 IOT devices (realized by Tencent continuous control switch)
剑指Offer04. 二维数组中的查找【中等】
Display time with message interval of more than 1 minute in wechat applet discussion area
Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
Detailed explanation of the most complete constraintlayout in history
随机推荐
如何在微信小程序中获取用户位置?
Is it safe to open an account for online stock speculation? Who can answer
【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
Write a simple nodejs script
Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
Keep learning swift
How to stand out quickly when you are new to the workplace?
With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?
Attack and defense world mobile--ph0en1x-100
Low code platform international multilingual (I18N) technical solution
Adult adult adult
Oh my Zsh + TMUX installation
Adult adult adult
Apache Mina Development Manual
公纵号发送提示信息(用户微服务--消息微服务)
2.6 preliminary cognition of synergetic couroutines
How to convert a decimal number to binary in swift
JVM memory model
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
雲計算未來 — 雲原生