interface

Interface: An
interface is a collection of functions, which can also be regarded as a data type, which is a more abstract "class" than an abstract class.
The interface only describes the methods it should have, and does not have a specific implementation. The specific implementation is done by the implementation class of the interface. This separates the function definition from the implementation and optimizes the program design
definition:
Unlike the class that defines the class, the interface keyword needs to be used when defining the interface.
We regard the interface as a special class
definition format that only contains function declarations:

public interface 接口名 {
抽象方法1;
抽象方法2;
抽象方法3;
}

Use interface to replace the original class, and other steps are the same as defining a class:
the methods in the interface are all publicly accessible abstract methods. The
interface cannot define ordinary member variable
classes. The implementation interface:
the relationship between the class and the interface is the implementation relationship, that is, the class implementation interface. The implemented actions are similar to inheritance, but the keywords are different. After implementing the interface using the implements
implementation class, it is equivalent to declaring: "I should have the functions in this interface".
Implementing classes still need to override methods to implement specific functionality.
Format:

class 类 implements 接口 {
    重写接口中所有方法
}

After the class implements the interface, the class will inherit the abstract method in the interface. At this time, the class needs to rewrite the
function defined in the abstract method interface. When it needs to have the function, the class can implement the interface and only declare In order to have this method, it is the declaration of the function
that overrides the method in the specific implementation class, and the implementation of the function is the specific implementation of the method.
Therefore, the declaration and implementation of the function are separated through the interface and the implementation class.
The characteristics of the members in the interface
1. Variables can be defined in interfaces, but variables must be modified by fixed modifiers, public static final, so variables in interfaces are also called constants, and their values ​​cannot be changed
2. Methods can be defined in interfaces, and methods also have fixed modifiers, public abstract
3. The interface cannot create objects
4. The subclass must override all the abstract methods in the interface before the subclass can instantiate
the multi-implementation of the
interface. The multi-implementation of the interface is to solve the drawbacks of multiple inheritance. Inheritance solves
multiple inheritance through multiple implementations of interfaces. When two parent classes have the same method, there will be security problems. When multiple implementations are implemented, because the functions in the interface do not have method bodies, the fundamental reason for
having subclass definitions is that inheritance has method bodies. The difference between interface and abstraction: the same point: both are located at the top of inheritance and are used to be implemented or inherited by other classes and cannot be directly instantiated. Objects contain abstract methods, and their subclasses must override these abstract methods Difference: abstract classes are part of Methods provide implementation, avoid subclasses from repeatedly implementing these methods, and improve code reuse Interfaces can only contain abstract methods A class can only inherit one parent class but can implement multiple interfaces










Abstract class is what should be in this thing, inheritance system is a kind of is..a relationship Interface
is the extra content in this thing, inheritance system is a kind of like..a relationship
.
Use less abstract classes,
you need to define the behavior of subclasses, and only choose abstract classes when you need to provide common ××× for subclasses

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324605011&siteId=291194637