Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C-inheritance


May 11, 2021 C#


Table of contents


The C#inherits

Inheritance is one of the most important concepts in object-oriented program design. I nheritance allows us to define another class based on one class, which makes it easier to create and maintain applications. It also helps reuse code and saves development time.

When creating a class, the programmer does not need to completely rewrite the new data members and member functions, only 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 inherited idea implements the belonging (IS-A) relationship. For example, mammals belong to (IS-A) animals, dogs belong to (IS-A) mammals, and therefore dogs belong to (IS-A) animals.

Base and derived classes

A derived class can have only one direct base class, but a base class can have more than one direct derived class. I nheritance can be passed. W hen you define a class to derive from another class, the derived class implicitly gets all members of the base class except its constructors and finalizers. D erived classes can therefore reuse the code in the base class without having to re-implement it. I n a derived class, you can add more members. In this way, derived classes extend the functionality of the base class.

The syntax for creating derived classes in C# is as follows:

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

Suppose you have a base class, Shape, whose 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 and executed, it produces the following results:

总面积: 35

Initialization of the base class

The derived class inherits the member variables and member methods of the base class. T he parent class object should therefore 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

Multiple inheritances

Multi-inheritance is not supported by C# . H owever, you can use interfaces to implement multiple inheritances. 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("油漆总成本: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }
}

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

总面积: 35
油漆总成本: $2450