当前位置:网站首页>Composition of interface
Composition of interface
2022-07-05 22:54:00 【Anny Linlin】
One 、 The interface consists of :
Constant public static final modification
Abstract method public abstract modification
The default method (Java8)
Static methods (Java9)
Private method (Java9)
Two 、 Default method in interface
The default method format definition in the interface :
public default return type Method name ( parameter list ){}
Example :public default void show3(){}
matters needing attention : The default method in the interface is not an abstract method , So don't force it to be rewritten , But it can be rewritten , Remove keywords when rewriting default That's it .public It can be omitted , however default Cannot be omitted .
When to use , When the newly created class implements the methods in the interface , When it comes to implementing new methods , Add a default method to the interface . The newly created class can override this method .
The code is slightly …………
3、 ... and 、 Static methods in the interface
Define the method format in the interface :public static return type Method name ( parameter list ){}
Example :public static void show()
matters needing attention : Static methods can only be called by interface name , Cannot call... By implementing class name or object name .public It can be omitted , however static Don't omit .
package staticFunction;
public interface Inter {
void show();
default void method(){
System.out.println("default");
}
static void test(){
System.out.println("test");
}
}
package staticFunction;
public class InterImpl implements Inter{
@Override
public void show() {
System.out.println("InterImpl show");
}
@Override
public void method() {
System.out.println("InterImpl method");
}
//test Static methods cannot be overridden in implementation classes
}
package staticFunction;
/*
1、 Define an interface Inter, There are three ways : One is the abstract method , One is the default method , One is the static method .
void show();
default void method();
static void test();
2、 Define the implementation class of an interface
InterImpl
3、 Define a test class
Create objects in a polymorphic way in the main method and use
summary : Static methods in interfaces can only use interface calls , Implementation classes cannot override , Unable to call .
*/
public class InterDemo {
public static void main(String[] args) {
Inter inter = new InterImpl();
inter.method();
inter.show();
//inter.test(); You cannot use an implementation class to call a method in an interface
Inter.test(); // Use the interface to call the static method of the interface
}
}
Four 、 Static methods in the interface
Java9 Private method with method body is added in , This is actually java8 The foreshadowing is buried in :java8 It is allowed to define the default method and static method with method body in the interface , This will cause a problem : When two static methods or private methods contain the same piece of code , The program must consider extracting this code into a method , And this common method cannot be known by others , So you need to hide it with private , This is a java9 The necessity of increasing private land .
The format of private method definition in interface :
package privateFunction;
public interface Inter {
default void show1(){
System.out.println("show1 Start ");
// System.out.println(" I love my country !");
// System.out.println(" I love the capital Beijing !");
show();
//method(); // Call static private methods
System.out.println("show1 end !");
}
default void show2(){
System.out.println("show2 Start ");
// System.out.println(" I love my country !");
// System.out.println(" I love the capital Beijing !");
show();
//method(); // Call static private methods
System.out.println("show2 end !");
}
private void show(){
System.out.println(" I love my country !");
System.out.println(" I love the capital Beijing !");
}
static void method1(){
System.out.println("method1 Start ");
// System.out.println(" I love my country !");
// System.out.println(" I love the capital Beijing !");
//show();// Non static methods cannot be used
method();
System.out.println("method1 end !");
}
static void method2(){
System.out.println("method2 Start ");
// System.out.println(" I love my country !");
// System.out.println(" I love the capital Beijing !");
//show();// Non static methods cannot be used
method();
System.out.println("method2 end !");
}
static void method(){
System.out.println(" I love my country !");
System.out.println(" I love the capital Beijing !");
}
}
package privateFunction;
public class InterImpl implements Inter{
@Override
public void show1() {
Inter.super.show1();
}
@Override
public void show2() {
Inter.super.show2();
}
}
package privateFunction;
/*
1、 Define an interface Inter, There are four ways : Two are the default methods , Two are static methods .
default void show1()
default void show2()
static void method1()
static void method2()
2、 Define an implementation class of the interface
InterImpl
3、 Define test classes :
InterDemo
Create objects in a polymorphic way in the main method and use
summary : Static methods in the interface can only call private static methods , Non static methods can call either private static methods or default methods
*/
public class InterDemo {
public static void main(String[] args) {
Inter inter = new InterImpl();
inter.show1();
inter.show2();
Inter.method1();
Inter.method2();
}
}
边栏推荐
- 鏈錶之雙指針(快慢指針,先後指針,首尾指針)
- Binary tree (III) -- heap sort optimization, top k problem
- 513. Find the value in the lower left corner of the tree
- APK加固技术的演变,APK加固技术和不足之处
- 70. Climbing Stairs. Sol
- QT creator 7 beta release
- Editor extensions in unity
- Golang writes the opening chapter of selenium framework
- Hcip day 12 (BGP black hole, anti ring, configuration)
- Postman核心功能解析-参数化和测试报告
猜你喜欢
![[groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)](/img/48/cd7960bbbc51a967b20da410bf81fe.jpg)
[groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)

My experience and summary of the new Zhongtai model

Codeforces Global Round 19

How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?

50. Pow(x, n). O(logN) Sol

513. Find the value in the lower left corner of the tree

Yiwen gets rid of the garbage collector

VOT Toolkit环境配置与使用

Vcomp110.dll download -vcomp110 What if DLL is lost

Metaverse ape received $3.5 million in seed round financing from negentropy capital
随机推荐
openresty ngx_lua请求响应
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Error when LabVIEW opens Ni instance finder
Thinkphp5.1 cross domain problem solving
70. Climbing Stairs. Sol
Navigation day answer applet: preliminary competition of navigation knowledge competition
Boring boring
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
链表之双指针(快慢指针,先后指针,首尾指针)
Metaverse ape ape community was invited to attend the 2022 Guangdong Hong Kong Macao Great Bay metauniverse and Web3.0 theme summit to share the evolution of ape community civilization from technology
d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
audiopolicy
點到直線的距離直線的交點及夾角
Codeforces Global Round 19
Activate function and its gradient
Binary tree (III) -- heap sort optimization, top k problem
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
3 find the greatest common divisor and the least common multiple