当前位置:网站首页>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.6 preliminary cognition of synergetic couroutines
- 写一个简单的nodejs脚本
- Swift bit operation exercise
- Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
- 4. 无线体内纳米网:电磁传播模型和传感器部署要点
- 【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
- Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation
- Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?
- Social community forum app ultra-high appearance UI interface
猜你喜欢
What is more elegant for flutter to log out and confirm again?
【ManageEngine】IP地址扫描的作用
最新版抽奖盲盒运营版
Ali & ant self developed IDE
Record your vulnhub breakthrough record
(latest version) WiFi distribution multi format + installation framework
Analysis of a music player Login Protocol
记录自己vulnhub闯关记录
T430 toss and install OS majave 10.14
雲計算未來 — 雲原生
随机推荐
Openstack node address change
Display time with message interval of more than 1 minute in wechat applet discussion area
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
Powerful avatar making artifact wechat applet
Sword finger offer03 Repeated numbers in the array [simple]
雲計算未來 — 雲原生
2020-09_ Shell Programming Notes
Ten workplace rules
公纵号发送提示信息(用户微服务--消息微服务)
Dix règles de travail
Lambda表达式
(最新版) Wifi分销多开版+安装框架
Airflow installation jump pit
社交社区论坛APP超高颜值UI界面
elastic_ L04_ introduction. md
Swift Error Handling
Is it safe to open an account for online stock speculation? Who can answer
Sword finger offer06 Print linked list from end to end
[download attached] password acquisition tool lazagne installation and use
elastic_ L02_ install