Because I personally have some Dongdong, I will give you a reference to you (the concept is the same) I will give an example here, maybe it's better to understand first, I will define an interface and some Class.
Interface car operation interface {
VoID throttle () {}
VoID brake () {}
Void steering wheel () {}
}
The following is a Class of some car, these ribs are the implementation of the vehicle, so the design method of the car's operation interface is different, but we still have one or one.
Class Ford_escape implements car operation interface {
VoID throttle () {
/ / Detailed fueling method for Escape
}
VoID brake () {
/ / Detailed brake method for ESCAPE
}
Void steering wheel () {
// For the detailed rotational steering wheel method for ESCAPE
}
}
Class Honda_CRV IMPLEMENTS car operation interface {
VoID throttle () {
// For CRV detailed fueling methods
}
VoID brake () {
/ / Detailed brake method for CRV
}
Void steering wheel () {
// For the CRV detailed rotation steering wheel method
}
}
Class Nissan_x_trail IMPLEments Car Operation Interface {
VoID throttle () {
// For X_Trail detailed fueling method
}
VoID brake () {
/ / Detailed brake method for x_trail
}
Void steering wheel () {
// For X_TRAIL detailed rotation steering wheel method
}
}
Now I will define an anki's class // Anki is a person, he will drive.
Public class anki {
/ / Assume that anki's driving method is as follows
// The parameter of the pass is the car operation.
// That is to say, if you want to call an ANKI, as long as you put there in the Implements car operation interface
// The car is given to me, I can open this car.
Public void driving (car operation face car_interface) {
Car_Interface. Gate ();
CAR_INTERFACE. steering wheel ();
CAR_INTERFACE. Brake ();
}
Public static void main (String [] args) {
Anki anki = new anki ();
Anki. Drive (New Honda_Crv ());
ANKI. Driving (New Ford_escape ());
ANKI. Drive (New Nissan_x_trail ());
}
}
From the example from the above example, although each car is different, it has a car's operation interface whether you give an ANKI, as long as there is this interface (car operation interface), Anki can take the car The car is very smooth (that is it to drive the car) Is there any example in Java? Yes, the following is a good example: Shape is an interface, there is a Class of IMplements with Area, Cubiccurve2D, GeneralPath, Line2D, Polygon, Quadcurve2D, Rectangle, Rectangulape from the literal meaning, the above Class is all Some IMPLEMENTS SHAPE this interface in graphics2d, there is such a Method: Abstract void draw (shape s) that is, as long as you give me a shape, I can draw it, without the need to manage these Shapes What kind of classes are. In other words, I can also draw more as long as it is a Class of the interface. Is this very convenient?