Java object oriented interface (1)
Java object oriented interface (1)
It's been a long time since the last article. I was busy reviewing near the end of the term. I also watched the interface for many days, checked a lot of materials, and the levels were uneven. Finally, I checked the introduction of the interface in the official documents, and then it became clearer. However, it is English after all. It seems that it is still relatively laborious, but after reading the whole article, there is an illusion that English has made rapid progress. Ha ha ha ha.
Without much to say, just give a vivid example:
The general meaning is that during development, each team only needs to recognize a "contract" for software interaction, and does not need to pay attention to how the code of other teams is written. The interface we will learn is to act as this "contract". In fact, it is well understood that the slogan of object-oriented is high cohesion and low coupling. The emergence of interfaces well separates the specification and implementation, which greatly reduces the coupling of components and greatly improves the scalability and maintainability. Then, the interface is a specification that other components must abide by. It is a group of public methods. It is really similar to the abstract classes we talked about before. In fact, they achieve similar functions. We will analyze the differences between the two in detail later. Next, we should explore the interface in many parts. Please look forward to it.
Definition format in interface
package com.my.pac19;
//Output.java
/**
* @auther Summerday
*/
public interface Output {
//常量: public static final int MAX_CACHE_LINE=50;
int MAX_CACHE_LINE = 50;
//普通方法: public abstract void out();
void out();
void getData(String msg);
//默认方法: public default void print(String...msgs)
default void print(String...msgs){
for(String msg:msgs){
System.out.println(msg);
}
}
default void test(){
System.out.println("default test()");
}
//静态方法:public static String statictest()
static String statictest(){
return "the static method in interface";
}
//!false:static String staticTest1();
}
The interface keyword is used to represent the interface. The interface can be regarded as a special class with similar naming conventions. Moreover, there can be at most one public interface in a java source file, and the Java source file name should be the same as the public interface name.
[修饰符] interface [接口名] extends [父接口1],[父接口2] {
//零到多个常量定义
//零到多个抽象方法定义
//零到多个默认方法和类方法定义
//零到多个内部类、接口、枚举定义
}
Member variables in interface
The interface can only have static variables, not instance variables, because it cannot create objects, so it cannot have instances.
//常量: public static final int MAX_CACHE_LINE=50;
int MAX_CACHE_LINE = 50;
Common methods in interfaces
Before Java 8, only abstract methods were allowed to be declared in the interface. After Java 8, enhancements were made to allow the declaration of default methods and class methods. Here, those other than those two are called ordinary methods, that is, abstract methods.
//普通方法: public abstract void out();
void out();
void getData(String msg);
Default method in interface
As the default method added after Java 8, the so-called existence is reasonable. Its emergence must solve some thorny problems, but at present, I don't understand it thoroughly. In the later study, I will understand it slowly and make a summary. Let's first understand its definition format:
//默认方法: public default void print(String...msgs)
default void print(String...msgs){
for(String msg:msgs){
System.out.println(msg);
}
}
//!false:default void test1();
//extension method should have a body
Static methods in interfaces
Similarly, both he and the default method are newly introduced babies. The specific advantages will be analyzed later. Let's first look at the definition:
//静态方法:public static String statictest()
static String statictest(){
return "the static method in interface";
}
//!false:static String staticTest1();