The relationship between interfaces and classes _ What is the difference between interface functions and calling functions
Classes and interfaces are two concepts that cannot be confused. The program interface is one of two types of interfaces provided by the operating system for the user. The programmer requests the operating system to provide services through the program interface in the program. The most basic unit for procedural languages ​​is procedures and functions. The interface, in layman's terms, is a concept for accomplishing an operation in which a function method can be defined. After the interface is implemented, the specific code operation of an operation is rewritten. It is to classify the inside of the class. For map objects, we can do several interfaces, which define methods, functions, and properties for different functions. The map class implements these interfaces, so that we can use interface definitions to implement objects. Therefore, an interface is a definition of a set of related methods and property sets. The interface is not limited to being a keyword, but a set of design ideas: · Clearly understand the interface of the object to be developed, which determines your understanding of the requirements and the correct direction of the design. In fact, it is the requirements analysis and summary design, which also determines the quality of the test design. · Clear the internal interface of the object (divide the module, clarify the interaction between them), determine the implementation of the program, in fact, the detailed design and coding work. · The code is messy, it is difficult to understand, then use interface separation, this is called refactoring. How can I describe the interface clearly? The description interface has two main points, called 2P: one is the protocol, and the other is the prototype (Prototype), which describes the way and content of the interaction. The protocol says how the caller and the callee interact, for example, based on the HTTP protocol, the request parameters are in urlencoded format, and the returned content is in JSON format; the prototype describes the contents of a request, the call name, parameters, and return content are What is the meaning, as well as the type. From software engineering to writing a function that goes to the background to get an order list, most of the time is spent on determining the interface and implementing the interface. The purpose of the interface: the commonality of the abstract class, can also be summarized as: for polymorphism. 1. The interface can only contain methods. (method, attribute, indexer, event) 2. The method in the interface cannot have any implementation. 3. Members in the interface cannot have any access modifiers (even public) 4. The interface cannot be instantiated 5. The class that implements the interface must implement all members of the interface (here as the abstract class) 6. Classes cannot be inherited more, so in some cases, only interfaces can be used instead. Interfaces can be implemented multiple times (a class can inherit multiple interfaces, but only one class can be inherited) 7. The main purpose of the interface is to achieve polymorphism 8. When a class inherits a class at the same time, and also implements some interfaces, you must write the inherited class in the first one (if a class inherits both the parent class and the interface, you must The parent class is written at the top) 9. When multiple classes have one or several functions (methods), but these types do not belong to the same series (these types do not have a common parent class, you cannot use abstract classes.) So, at this time, in order to achieve polymorphism, you can consider extracting the methods shared by these types into an interface, and let these types implement the interface separately. 10. When the parent class implements the interface, the child class inherits the parent class. Then the interface can reference subclasses 11. Do not write too many methods in an interface to avoid interface pollution. Multiple interfaces can be written, and in each interface, methods are defined separately 12. When implementing the interface method, don't have the override keyword, just like the usual definition. 13. Display implementation of the interface method, if there are the same method name in multiple interfaces. Then the first method name defaults to the top interface. If you want to use the method of the interface, you need to display the implementation: interface name. Method name. Access modifier is private When do you put the method in the parent class or in the interface: When a parent class derives 5 subclasses, only 3 of all subclasses require a function, that is, not all subclasses need this function, then you can put the subfunction into an interface, each subclass Perform a separate implementation. Interfaces are better for constraining subclasses. The basis for implementing information encapsulation (data members and member functions). A class is a user-defined type, also known as a class type. Each class contains a data description and a set of operational data or functions that deliver the message. An instance of a class is called an object. a: class and class * inheritance relationship b: class and interface * implementation relationship c: interface and interface * inheritance relationship [java] View plain copy Interface interA{ Public abstract void printA(); } Interface interB{ Public abstract void printB(); } Interface interC extends interA,interB{} Class demo3 implements interA,interB{ / / Interface breaks the single inheritance in java Public void printB() { // TODO Auto-generated method stub } Public void printA() { // TODO Auto-generated method stub } } Encapsulating data and operations as an organic whole, because the private members in the class are hidden and only provide a limited interface to the outside, it can guarantee high internal cohesion and low coupling with the outside. Users do not have to know the specific implementation details, but just use the external interface to use the members of the class with specific access rights, which can enhance security and simplify programming. Inheritance is more in line with cognitive rules, making the program easier to understand while saving unnecessary duplication of code. The same operation acts on different objects and can have different interpretations, resulting in different execution results. At runtime, you can call methods in a derived class by pointing to a pointer to the base class. Class classifications are: abstract class, sealed class, static class (staTIc) usage Define a class Class class name { Public: Public member Private: Private member Protected: Protect member }; Public members, private members, and protected members all contain data members and member functions, which are not in order of each other. A public/private/protected keyword can be followed by multiple members until the next public/private/protected keyword. If the member does not have a public/private/protected keyword before it, it defaults to a private member. The semicolon at the end is essential, otherwise compilation errors will occur. Whether public, private, or protected, they are accessible to each other. For example, a public member function can manipulate a protected data member or a private member function. The data member of the class is a type, so it cannot be assigned. The declared data member has the same format as the declared ordinary variable, such as "int n;". Shenzhen Kaixuanye Technology Co., Ltd. , https://www.iconline-kxy.com
Can only inherit from one, can inherit in multiple layers
Classes can be implemented in a single implementation or multiple implementations, and multiple interfaces can be implemented while inheriting a class. The interface cannot be instantiated, the main purpose is to achieve polymorphism. A class can only have one parent class, an interface can inherit multiple interfaces, and the methods in the interface are abstract methods (no method body).
Can be single inheritance, can inherit more