C# Tutoriallogin
C# Tutorial
author:php.cn  update time:2022-04-11 14:06:23

C# inheritance



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 the derived class.

The idea of ​​inheritance realizes the belongs(IS-A) relationship. For example, mammal belongs to (IS-A) animals, dog belongs to (IS-A) mammals, so dog belongs to (IS-A) animal.

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:

<acess-specifier> class <base_class>
{
 ...
}
class <derived_class> : <base_class>
{
 ...
}

Assume 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;
   }

   // 派生类
   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);

         // 打印对象的面积
         Console.WriteLine("总面积: {0}",  Rect.getArea());
         Console.ReadKey();
      }
   }
}

When the above code is compiled When executed, it will produce the following results:

总面积: 35

Initialization of the 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;
      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("成本: {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:

长度: 4.5
宽度: 7.5
面积: 33.75
成本: 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 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("油漆总成本: 
总面积: 35
油漆总成本: 50
" , Rect.getCost(area));          Console.ReadKey();       }    } }

When the above code is compiled and executed, it produces the following results:

rrreee

php.cn