Inheritance is one of the most important concepts in object-oriented programming. Inheritance allows us to define a class based on another class, which makes it easier to create and maintain applications. It also helps reuse code and save development time.
When creating a class, programmers do not need to completely rewrite new data members and member functions. They only need to design a new class and inherit the members of the existing class. This existing class is called the base class, and the new class is called a derived class.
The idea of inheritance realizes the Belongs to (IS-A) relationship. For example, mammals are (IS-A) animals, dogs are (IS-A) mammals, so dogs are (IS-A) animals.
Base classes and derived classes
A class can be derived from multiple classes or interfaces, which means it can inherit data and functions from multiple base classes or interfaces.
The syntax for creating a derived class in C# is as follows:
Suppose 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;} // Disposter class Rectangle: Shape {PublicInt Getarea ( ) {RETURN (Width*Height);}} Class Rectangleteter {Static voidMain(string[] args) { Rectangle Rect = new Rectangle(); Rect.setWidth(5); Rect.setHeight(7); //Print the area of the object Console.WriteLine("Total area: {0 }", Rect .getArea()); Console.ReadKey(); } }}
When the above code is compiled and executed, it will produce the following results:
Total area: 35
Initialization of base class
The derived class inherits 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 { // 成员变量 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("长度: {0}", length); Console.WriteLine("宽度: {0}", width); Console.WriteLine("面积: {0}", GetArea()); } }//end class Rectangle class Tabletop : Rectangle { private double cost; publicTabletop(double l, double w) : base(l, w) { } public double GetCost() { double cost; cost = GetArea() * 70; return cost; } public void Display() { base.Display(); Console.WriteLine("成本: {0}", GetCost()); } } classExecuteRectangle { static void Main(string[] args) { Tabletop t = new Tabletop(4.5,7.5); t.Display(); Console.ReadLine(); } }}
当上面的代码被编译和执行时,它会产生下列结果:
长度: 4.5宽度: 7.5面积: 33.75成本: 2362.5
C# 多重继承
多重继承指的是一个类别可以同时从多于一个父类继承行为与特征的功能。与单一继承相对,单一继承指一个类别只可以继承自一个父类。
C# 不支持多重继承。但是,您可以使用接口来实现多重继承。下面的程序演示了这点:
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; } // 基类 PaintCost public interface PaintCost { int getCost(int area); } // 派生类 class Rectangle : Shape, PaintCost { public int getArea() { return (width * height); } public int getCost(int area) { 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(); // 打印对象的面积 Console.WriteLine("总面积: {0}", Rect.getArea()); Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area)); Console.ReadKey(); } }}
当上面的代码被编译和执行时,它会产生下列结果:
总面积: 35油漆总成本: $2450
更多C# 继承相关文章请关注PHP中文网!