White horse
----inherit
Author: HolyFire
The ancients: "White Malaysia", why is it?
There is a payment here, and the white horse refers to a white horse, not the horse in the horse, is a variety of horses, black horses, brown, five or more, this is the difference in color, The horse is also a place, variety, and the like. Here is a revelation, a variety of horses can find a lot of common, calling the horse, animals in line with these characteristics are horses, so these common points can not be more, but more All horses, less expression, do not speak the horse. To distinguish a horse to need a lot of information, such as a black Mongolian horse, here, in addition to describing a horse, I have to point out that it is black, the origin is Mongolia, and there is no higher variety of horses.
I have been mentioned in "Mustic ----- Package", and things can be divided into a collection of properties and methods. So, can we process this collection, we only need to add something else, then he can become other things, imagine that in the body of the horse, hehe, hehe, Ridiculous. Then we say some serious, know the totem of our Chinese nation, dragon! The dragon is on the top of the snake, plus the corner of the deer, plus the tiger's leg, the tail is the tail of the fish, and so on. But it is not easy to make the great symbol of the dragon, and it will become a seduction of the snake.
Although it is simple and added, there are different ways. It is generally two ways: correlation and derivation.
Association: Suppose the original class is to use a bag to put the properties and methods, which is used to describe the relationship between multiple classes, the simplest case is to pack two small bags with a larger bag. Become a new class. This association is called a combination.
Derivative: Derivation is to use a large bag to bring a small bag with some attributes and methods to become a new class.
A
C
B
A: Association
A
Di
B: Derivative
I still use C to tell.
Class a {
// ... attribute method
}
Class b {
// ... attribute method
}
// Related new class C
Class C {
A a a;
B B;
}
// Derived new class D
Class D: public a {
// ... attribute method
}
It seems that the association (combination) is more likely to understand, that is more than a class, then forms a new class, which is nothing difference with the original class, the class defined is a type, which is the package to do.
Derivative - Based on the original class, add some attributes and methods to generate a new class, look novelty, sprinkle some sesame on the bread, eat more fragrant, isn't it.
How to use this method is what I want to say - inheritance, as long as you do it, the bread will be very delicious. (He mentioned here is the form of Class Y: public x;
The above method is simple, if you use it, it is not easy.
First of all, D and A have the same part to a
Second, D has a part of a
Both light, these two points also explain the use of inheritance
We plus a new class E, he is also derived with class A
Class E: public a {
// ... attribute method
}
In this way, we will be clear, A is the commonality of D and E
Jer ~~~~! We finally found the benefits of this.
The class D and class E have the same part A, which is only handled once, which means that we can process a one-size job like A, if many classes are derived from A, then us It's developed, the problem is how to make more classes can be derived from class A to be useful and easy to use. Here, the old ancestors have also been fascinated, and the white horse is said to be this truth. Ma is this class A, plus color, origin, variety, you can describe a variety of horses, plus health, etc. The state of the horse, such as a living black Mongolian horse.
There are a lot of known common, bowls, porcelain, wind, people, people, and flowers, and we will collect them.
We can define the next definition: inheritance - is the same and similar part between things.
Let's take a look at the relationship between base categories A and subclass D, which can be found in D, which means that a Class A object can be extracted from a D class object, this phenomenon is the inheritance of downwardness The horse can be white horses, while white horses can't be said to be a representative of horses. How to deal with C ?
D D;
A a = d;
Here, a temporary variable of a type A is produced, which is composed of D in class A.
[
Composition of D
[Part of class A] - à temporary variable DA;
...
]
A a = da;
This phenomenon is called slice
Look another situation again
D * d = new d;
A * a = D;
At this time, A * a = d; only involve the assignment of the pointer, there is no object creation, so it does not cause a slice, here is the number of adds D in memory in a pointer, and describing the pointer points to the object is a The object of class A. Due to the information of the associated A, the C compiler makes such an operation, it is also understandable.
We can call the D of the inheritance A as a type of A, a kind of class A (or said, D is a a), like a white horse is a horse (white horse is a horse).
Inheritance is very powerful, use it to get a lot of benefits, but there is no inclusion in the world, and the use of inherits will also cause trouble. It can be seen that inheritance is a static relationship. It is set to have a lot of things during compilation. This also improves efficiency, but it is difficult to change, so flexibility is not enough, you want to clear, what is your purpose, Decide whether to use the association or inherit.
2001/8/15
Ding Ning