Detailed JAVA Interface

Functions as an interface: can be realized design and implementation separation, abstract common than N different types.

For example: aircraft, birds, balls, missiles, spacecraft ......

Inheritance: is -a relationship triangle is a geometric

Bird is a ball ? Does not hold

Interface : has -a between the phone has a camera function

The plane has a flying capability

Bird has a flying capability

Interface is a manifestation of the ability to

 

Acquaintance Interface

If a class all methods are abstract methods, then this class can be declared as an interface

public abstract class MyClass { 
public abstract void show(); 
public abstract String fun();
public abstract int [] method(); 
}

 

The following code can be changed

public interface MyInterface { 
void show(); 
String fun(); 
int [] method(); 
}

 

Definition of the interface: the use interface modification, a data type, data type reference

Interface definition :

public interface MyInterface { 
void show(); 
}

 

What interface can contain?

 

(1) an abstract method

public abstract method

(2) non-abstract methods

/**JDK1.8新特性 ,必须使用default关键字*/ 
public default void method2(){ 

}

 

(3) Properties ( public static Final ) Constant , public static final can be omitted

public interface MyInterface {
String name="张三"; public static final String name2="张三"; }

Note: Can contain constructors interface it? It is not allowed , because the interface is not a class

 

 

Published 127 original articles · won praise 7 · views 20000 +

Guess you like

Origin blog.csdn.net/ZGL_cyy/article/details/104182232