26 c # class combination,
A combination combines all parts. In program design, existing class objects are used to generate new classes.
The table is made up of wooden boards and nails. The desk lamp is made up of lamps, tubes, wires, and connectors. We found that many things around us are made up of smaller things, just like building blocks. I believe that when you were a child, you had opened many things that you felt curious about and checked them out. Let's see if this new class is made up of other classes. In fact, when you were very young, you already understood the combination.
In the program design, the combination is embodied in the generated new class and the existing class instance is used. Combination makes it easier and more intuitive to generate new classes, and easier to implement. Most people (including in real life) prefer combination, compared to the inheritance method that constructs new classes through existing classes.
I made a practical example to help you understand the power of a combination. We already have two classes. One is Cooler with the function of blowing cold wind, and the other is Heater with the function of blowing warm wind. Now we want to combine these two types of functions to form a new class AirConditionerCoolAndHeat, a heating and cooling air conditioner with cooling and heating functions. How can this problem be solved? Add two field members to the new class. One is a Cooler type object and the other is a Heater type object. Call the Coolder method in the cooling function. Call the Heater method in the heating function. How about cool.
You create a project. The solution name is Sloution26, the project name is Exercise26, and the project type is console program. Add three new class files. They are Cooler, Heater, and AirConditionerCoolAndHeat.
Click Cooler. cs and modify the Code as follows:
Click Heater. cs and modify the Code as follows:
Click AirConditionerCoolAndHeat. cs and modify the Code as follows:
Click Program. cs and modify the Code as follows:
Running result
Exercise
1. Read this code, add comments to important statements, and explain its functions.
2. Create a dehumidifier with the dehumidification Function Based on Cooler and Heater. Modify AirConditionerCoolAndHeat to perform dehumidification.
3. Use inheritance to enable Cooler to heat up.
4. Use inheritance to enable cooler with heating and dehumidification functions.
Pay-as-you-go answers to questions C # encountered during the course of getting started.
Zero-basic learning C # beginners, most problems can be solved through self-study or the Internet.
HoweverThere are still some problems that are not so easy to solve and become a stumbling block to your learning. In the end, it may even cause you to lose your learning experience. Generally, when you get started with C # programming, you will encounter more than 20 to 30 problems that you cannot easily solve.
What do you need most at this time?
Yes! A professional teacher will help you!
If you have any questions during this course, you can ask the author for help via qq or email.
Only 30 RMBYou can get three professional answers from the author of this tutorial. So you only need less than 300 yuan to successfully complete the C # entry, and then open the door to the software industry.
The payment method is very simple:
Scan the following QR code. Leave your QQ number or email address in the payment message.
Education is not cheap, but reliable! Truly smart people know how to spend money to make professional people serve themselves and improve their efficiency.
Instructor qq: 2040227296
Instructor email: 2040227296@qq.com