C # class inheritance

Source: Internet
Author: User

C # class inheritance is the process of assigning new classes from an existing class.
Through inheritance, the new class automatically includes the variables and methods in the original class. Then, crop the class as needed,ProgramYou can add new variables to the derived class or modify the inherited class.
In general, new classes can be created faster, easier, and cheaper through inheritance than newly written classes. Inheritance is a way to support software reuse. (The purpose of inheritance is to reuse existing software ).
However, according to some materials recently, in inheritance, the subclass does not inherit the private member of the parent class. This statement should be incorrect. Let's clarify that in inheritance, all the methods and variables defined in the parent class, and even those private (prviate) members, are also inherited by the subclass. However, these private members cannot be directly referenced by class names or objects. But they can be indirectly referenced.

Let's take an example to demonstrate this situation.

1 Using System;
2 Using System. Collections. Generic;
3 Using System. text;
4
5 Namespace Foodanalyzer
6 {
7 /**/ /*The fooditem class indicates the general type of food. The fooditem constructor receives fat grams and food scores.
8* The claories method returns the calorie Size Based on fat. It triggers the caloriesperserving method to help calculate
9* Calculate the size of each fat calorie.*/
10 Public   Class Fooditem
11 {
12 Const   Int Calories_per_gram =   9 ;
13 Private   Int Fatgrams;
14 Protected   Int Servings;
15 Public Fooditem ( Int Numfatgrams, Int Numservings)
16 {
17Fatgrams=Numfatgrams;
18Servings=Numservings;
19}
20 Private   Int Calories ()
21 {
22ReturnFatgrams*Calories_per_gram;
23
24}
25 Public   Int Caloriesperserving ()
26 {
27Return(Calories ()/Servings );
28}
29 }
30 /**/ /*The pizzs class is derived from the fooditem class, but it does not add special functions or data. Its constructor uses the base
31* The reference string uses the fooditem constructor and declares that each pizza has 8 copies.*/
32 Public   Sealed   Class Pizza: fooditem
33 {
34 Public Pizza ( Int Fatgrams ): Base (Fatgrams, 8 )
35 {
36 
37}
38 }
39 /**/ /*The main method instantiates a pizza object and triggers a method to determine how many calories each pizza contains based on its fat content.*/
40 Class Foodanalyzer
41 {
42 Static   Void Main ( String [] ARGs)
43 {
44 Pizza special =   New Pizza ( 275 );
45 Console. Out. writeline ( " Calories per serving: " + Special. caloriesperserving ());
46 Console. In. Readline ();
47 }
48 }
49 }
50

The pizza pair called special in the main method is used to trigger the caloriesperserving method (public). It is defined as the public method of fooditem, so the object of pizza must inherit this method. Note that caloriesperserving calls calories, which is declared as private visibility. In addition, calories references the variables fatgrams and the constant calories_per_gram, which are also declared as private visibility.
Even if the pizza class cannot display reference calories, fatgrams, or calories_per_gram, they can be used indirectly when pizza objects require them. The pizzade object cannot be used to trigger the calores method, but it can use a method that can trigger it. Note that the fooditem object is not created or needs to be deleted from this instance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.