Https://mp.weixin.qq.com/s/tEVw0j18rX2hxt-guXQJ-w
The essence of the road to Jane is often very simple.
Nature:
The class is the subject, and the interface is the protocol of communication between the subjects.
Position:
1. Both sides of the main station, the pipe connecting the interface between the two;
2. The interface is located on the main side;
3. The parent class is above the head of the principal class, and the parent class is an abstraction of the conceptual hierarchy of the child class (the concept is generalized);
The relationship between the interface and the subject:
1. Two communication main body, one uses the interface, one realizes the interface;
2. The interface is the set of actions, realizes the interface, indicates the main body has the ability to execute the interface convention action;
3. The body that uses the interface does not need to implement the interface, but needs to know the interface:
A. If you do not know the constant of the agreement, then the two parties ' meaning will be confused;
B. Do not know the method signature will be denied service;
C. Do not know the sequence of method calls, will be denied service;
D. The first step in the use of the interface can be authentication, without the corresponding information will be rejected;
4. The interface is an integral part of the body:
A. The interface is the ability to implement the main body of the interface;
B. The interface is the knowledge of the subject of the interface, or the use of an interface is also a capability;
5. The main body can have a variety of capabilities, a variety of communication, so can achieve a plurality of interfaces;
Protocol:
Basic:
1. Constants are used to agree that both parties refer to the same thing;
2. Method signature to agree what to do if you want to do something, what you need to provide;
Combination of:
3. If the communication requires more than one step, the order of the calling method needs to be agreed;
This specification Java interface cannot be implemented, need to use the document to explain and specification;
Example:
1. Subject classmate A and the main classmate B;
2. Main body B has ice cream, popsicle, with the ability to sell ice cream, popsicle;
3. Body B marks the ice cream as the first commodity, Popsicle marked as the second commodity;
4. The subject armor is rich;
5. Body a knows the subject B sells things;
6. The body armor knows that the first product is ice cream;
7. Subject a The money to the main body B, and told him to buy the first commodity;
8. Subject B confirms that the money is sufficient and delivers the first commodity to the subject armour;
9. Body armor to get ice cream;
Java Description:
1. Manjia, Manyi;
2.
InterfaceSeller {intPRODUCT_BQL = 0; intPRODUCT_XG = 1; Product Sell ( money money,intproductnumber);}classManyiImplementsSeller {Product sell,intProductNumber) { if(Ismoneynotenough (Money)) {return NULL; } if(ProductNumber = =product_bql) { returnfindbql (); } }}
4.
class Manjia { = dad.give (+); = Find.found (); Public void Buy () { = Seller.sell (money, SELLER.PRODUCT_BQL); }}
Parent class and interface:
1. The parent class and interface are different concepts, different concepts and different locations;
2. Subclasses are abstracted as a whole, ascending a conceptual hierarchy (generalization), becoming the parent class;
An interface is an abstraction of a subset of sub-class capabilities;
3. The concept of the parent class is broader than the subclass, and the subclass is conceptually incapable of being divorced from the category of the parent category: The White Horse is always a horse;
Interface is a kind of ability to implement the subject, from the perspective of this ability, the subject is the concept of interface to express:
The subject B is the seller when selling things;
Why interfaces are preferred rather than abstract parent classes:
1. Prioritize from a certain point of view, rather than a holistic definition, leaving leeway and facilitating expansion;
The nature of the java-interface