JAVA object-oriented interface

One, JAVA object-oriented-------interface

Now I have to fly from Beijing to Urumqi to eat Xinjiang kebabs; airplanes, birds, and superman are not the same kind, but they all have the same characteristic - they all fly. Introduce a new concept here-interface.
-Can be used to standardize the implementation of the interface must implement the abstract method in the interface.

Second, use steps

1. Definition

The code to define the interface is as follows:

public interface 会飞的 {
    
    
public void 起飞(); //无具体实现,也无法定义具体实现,这里是抽象方法
public void 巡航飞行();
public void 降落();
}

1. The abstract keyword in the abstract method cannot be omitted, but the method declaration in the interface can add abstract or not.
2. The interface cannot be used directly, there must be a corresponding implementation class

Code:

public class 飞机类 implements 会飞的 {
    
     //共性是通过实现接口来表示的
private String name; //名称,这是类属的属性,这里可以定义字节类的成员,和接口无关
//如果当前类不是抽象类,则必须实现接口中的所有抽象方法
public void 起飞() {
    
    
System.out.println("使劲的跑,一抬头就飞起来了");
}
public void 巡航飞行() {
    
    
System.out.println("使劲的烧油...");
}
public void 降落() {
    
    
System.out.println("我对准...");
}
}

Variables are defined through interfaces, and instances of specific classes are used to make calls;
multiple inheritance is not directly supported in Java. Because of the uncertainty of calling, Java has improved the multiple inheritance mechanism and has become multiple implementations in Java. A class can only have one direct parent class, a class can implement multiple interfaces, and an interface can inherit multiple interfaces.

The interface has no construction method, and cannot define static or non-static code blocks; without attributes, only constants can be defined. The defined constants can be used directly; the constants can also be used through the implementation class.

The interface may or may not contain abstract methods.


Guess you like

Origin blog.csdn.net/weixin_42437438/article/details/112850543