In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
C++ 's "inheritance" feature can improve the reusability of programs. It is precisely because "inheritance" is too useful and easy to use that it is necessary to prevent the misuse of "inheritance". We need to set some rules for the use of inheritance:
First, if class An and class B are not related, you can't let B inherit the function of An in order to make B have more functions.
Do not think that "do not eat for nothing", let a healthy young man eat people to replenish his body for no reason.
Second, if it is necessary for class B to use the function of A, it should be considered in two cases:
(1) if B is logically a "a kind of" of A, B is allowed to inherit the function of A. For example, Man is a kind of Human, while Boy is a kind of man. Then class Man can be derived from class Human, and class Boy can be derived from class Man. The example program is as follows:
Class Human
{
...
}
Class Man: public Human
{
...
}
Class Boy: public Man
{
...
}
(2) if An is logically a "a part of" of B, B is not allowed to inherit the function of A, but to combine B with An and other things. For example, the Eye, Nose, Mouth, and Ear are part of the Head, so the class Head should be composed of classes Eye, Nose, Mouth, and Ear, not derived. The example program is as follows:
Class Eye
{
Public:
Void Look (void)
}
Class Nose
{
Public:
Void Smell (void)
}
Class Mouth
{
Public:
Void Eat (void)
}
Class Ear
{
Public:
Void Listen (void)
}
/ / correct design, lengthy program
Class Head
{
Public:
Void Look (void) {m_eye.Look ();}
Void Smell (void) {m_nose.Smell ();}
Void Eat (void) {m_mouth.Eat ();}
Void Listen (void) {m_ear.Listen ();}
Private:
Eye m_eye
Nose m_nose
Mouth m_mouth
Ear m_ear
}
If Head is allowed to derive from Eye, Nose, Mouth, Ear, then Head will automatically have the functions of Look, Smell, Eat, Listen:
/ / wrong design
Class Head: public Eye, public Nose, public Mouth, public Ear
{
}
The above program is short and works correctly, but the design is wrong. At the beginning, many programmers can't stand the temptation of "inheritance" and make design mistakes.
A rooster chased a hen that had just laid eggs. Do you know why?
Because the hen laid duck eggs.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.