当前位置:网站首页>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 .
边栏推荐
- RedHat5 安装Socket5代理服务器
- (最新版) Wifi分销多开版+安装框架
- 4. 无线体内纳米网:电磁传播模型和传感器部署要点
- Powerful avatar making artifact wechat applet
- Attack and defense world mobile--ph0en1x-100
- 1-1 token
- Sword finger offer10- I. Fibonacci sequence
- Social community forum app ultra-high appearance UI interface
- 阿里 & 蚂蚁自研 IDE
- Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?
猜你喜欢
GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
Day 1 of kotlin learning: simple built-in types of kotlin
剑指Offer09. 用两个栈实现队列
最新版盲盒商城thinkphp+uniapp
Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?
Analysis of a music player Login Protocol
电压环对 PFC 系统性能影响分析
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
TOGAF认证自学宝典V2.0
基于同步坐标变换的谐波电流检测
随机推荐
What is more elegant for flutter to log out and confirm again?
I'm too lazy to write more than one character
Nodejs+express+mysql realizes login function (including verification code)
studio All flavors must now belong to a named flavor dimension. Learn more
ORM use of node -serialize
Day 1 of kotlin learning: simple built-in types of kotlin
Xctf mobile--app1 problem solving
Sword finger offer04 Search in two-dimensional array [medium]
【ManageEngine】IP地址扫描的作用
Sword finger offer05 Replace spaces
1-1 token
Swift5.7 扩展 some 到泛型参数
强大的头像制作神器微信小程序
TOGAF认证自学宝典V2.0
Node.js: express + MySQL的使用
JVM内存模型
Xctf mobile--app3 problem solving
Adult adult adult
Approve iPad, which wants to use your icloud account
2020-10_ Development experience set