当前位置:网站首页>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 .
边栏推荐
- Write a simple nodejs script
- Simple use and precautions of kotlin's array array and set list
- Xctf mobile--app1 problem solving
- 写一个简单的nodejs脚本
- Lambda expression
- Applet wxss introduction
- 1-1 token
- Computer version wechat applet full screen display method, mobile phone horizontal screen method.
- context. Getexternalfilesdir() is compared with the returned path
- Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
猜你喜欢

剑指Offer06. 从尾到头打印链表

Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.

Sword finger offer05 Replace spaces

Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!

If you can't learn, you have to learn. Jetpack compose writes an im app (II)

Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)

ncnn神经网络计算框架在香橙派OrangePi 3 LTS开发板中的使用介绍

The latest version of lottery blind box operation version

Use Tencent cloud IOT platform to connect custom esp8266 IOT devices (realized by Tencent continuous control switch)

Eureka self protection
随机推荐
Xctf mobile--app3 problem solving
电压环对 PFC 系统性能影响分析
Record your vulnhub breakthrough record
公纵号发送提示信息(用户微服务--消息微服务)
最新版盲盒商城thinkphp+uniapp
Sword finger offer03 Repeated numbers in the array [simple]
阿里 & 蚂蚁自研 IDE
记录自己vulnhub闯关记录
TOGAF认证自学宝典V2.0
Computer version wechat applet full screen display method, mobile phone horizontal screen method.
CNN MNIST handwriting recognition
repo Manifest Format
Self made pop-up input box, input text, and click to complete the event.
Adult adult adult
Node.js: express + MySQL的使用
低代码平台国际化多语言(i18n)技术方案
【综合题】【数据库原理】
Applet wxss introduction
Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!
Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation