1. Inheritance relationship (generalization relationship)
Description: The inheritance relationship is a subclass (derived class) that inherits the parent class (the base class), or the child interface inherits the parent interface's relationship. The subclass object "is a" parent object, such as a bird is an animal.
"UML Diagram":
Diagram: Animal is the parent class, Bird class, Fish class, dog class inherit the animal class, they not only inherit animal common method breath (), but also expand the relevant methods according to their actual needs (Fly () method, swim () method, Run ( ) method).
"Corresponding Code":
Animal Class (Parent Class): Class
Animal
{public
void breath () {}
}
//bird class, Fish class, and dog Class (subclass): Class
Bird: Animal
{public
void Fly () {}
}
class Fish:animal
{public
void swim () {}
}
clas S dog:animal
{public
void Run ()}
}
"The Finishing Touches":
★ The subclass can inherit all the private properties and methods of the parent class, and can expand according to the actual situation (add the attribute or method), as in the above example, the fly () method is the development of the animal species;
★ Class can only have single inheritance (a subclass can inherit only a parent class, a parent class can inherit from multiple subclasses), and an interface can have multiple inheritance (a sub-interface can inherit multiple parent interfaces).
2, the realization of relations
"description": Implementing a relationship is the relationship between the class implementation interface. The following is illustrated by the example in the "Liar design pattern".
"UML Diagram":
Graphic: Robot cat, Monkey King, Pig Eight commandments can be changed things, but because other animals do not have the same abilities as the three of them, it is not possible to add this method directly to the parent animal, but to abstract an interface called "Changing things". Then let the animals with this ability (such as Machine Cat, Monkey King, Pig Eight precepts) directly to implement this interface.