C # Inheritance
Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define a class by defining another class based on one class, which makes it easier to create and maintain applications. It also facilitates the reuse of code and saves development time.
When creating a class, the programmer does not need to completely rewrite the new data member and member functions, just design a new class, inheriting the members of the existing class. This existing class is called the base class, and this new class is called a derived class.
The thought of inheritance realizes the relationship of (IS-A). For example, mammals belong to (is-a) animals, dogs belong to (is-a) mammals, so that dogs belong to (is-a) animals.
base classes and derived classes
A class can derive from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces.
The syntax for creating derived classes in C # is as follows:
<acess-specifier> class <base_class>{...} Class <derived_class>: <base_class>{...}
Suppose that there is a base class Shape, and its derived class is Rectangle:
Using System;namespace inheritanceapplication{ class Shape {public void setwidth (int w) { width = W; } public void setheight (int h) { height = h; } protected int width; protected int height; } Derived classes class Rectangle:shape {public int getarea () { return (width * height); } } class Rectangletester { static void Main (string[] args) { Rectangle Rect = new Rectangle (); Rect.setwidth (5); Rect.setheight (7); The area of the printed object Console.WriteLine ("total area: {0}", Rect.getarea ()); Console.readkey ();}}}
When the above code is compiled and executed, it produces the following results:
Total area: 35
Initialization of the base class
Derived classes inherit the member variables and member methods of the base class. Therefore, the parent class object should be created before the child class object is created. You can initialize the parent class in the member initialization list.
The following program demonstrates this:
Using System;namespace rectangleapplication{class Rectangle {///member variable protected double length; protected double width; Public Rectangle (Double L, double w) {length = L; width = w; } public double Getarea () {return length * width; public void Display () {Console.WriteLine ("Length: {0}", length); Console.WriteLine ("Width: {0}", width); Console.WriteLine ("Area: {0}", Getarea ()); }}//end class Rectangle class Tabletop:rectangle {private double cost; Public tabletop (Double L, double W): Base (L, W) {} public double getcost () {double cost; Cost = Getarea () * 70; return cost; } public void Display () {base. Display (); Console.WriteLine ("Cost: {0}", Getcost ()); }} class Executerectangle {static void Main (string[] args) {Tabletop T = new Tabletop (4.5, 7.5) ; T.Display (); Console.ReadLine (); } }}
When the above code is compiled and executed, it produces the following results:
Length: 4.5 Width: 7.5 Area: 33.75 Cost: 2362.5
C # Multiple Inheritance
C # does not support multiple inheritance. However, you can use interfaces to implement multiple inheritance. The following program demonstrates this:
using System;namespace inheritanceapplication{class Shape {public void set width (int w) {width = w; } public void SetHeight (int h) {height = h; } protected int width; protected int height; }//base class Paintcost public interface paintcost {int getcost (int.); }//derived class class Rectangle:shape, paintcost {public int getarea () {return (width * height); } public int Getcost (int.) {return area * 70; }} class Rectangletester {static void Main (string[] args) {Rectangle Rect = new Rectangle (); int area; Rect.setwidth (5); Rect.setheight (7); Area = Rect.getarea (); The area of the printed object Console.WriteLine ("total area: {0}", Rect.getarea ()); Console.WriteLine ("Total Paint cost: ${0}", Rect.getcost (area)); Console.readkey (); } }}
When the above code is compiled and executed, it produces the following results:
Total area: 35 Total paint Cost: $2450
The above is the "C # tutorial" C # inherited content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!