1.35. C # inheritance

发布时间 :2023-10-12 23:00:06 UTC      

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

When creating a class, programmers do not need to completely rewrite new data members and member functions, they just need to design a new class thatinherits the members of the existing class. This existing class is called the base class, and this new class is called the derived class.

The idea of inheritance implements the IS-A relationship. For example, mammals belong to (IS-A) animals, dogs belong to (IS-A) mammals, so dogs belong to (IS-A) animals.

1.35.1. Base and derived classes #

A class can be derived from multiple classes or interfaces, which means thatit can inherit data and functions from multiple base classes or interfaces.

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

<Access modifier> class <Base class>
{
 ...
}
class <Derived class> : <Base class>
{
 ...
}

Suppose there is a base class Shape derived class is Rectangle :

Example #

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 class
   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);
         // Print the area of an 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

1.35.2. Initialization of base class #

The derived class inherits the member variables and member methods of the base class. Therefore, the parent object should be created before the subclass object is created. You can initialize the parent class in the member initialization list.

The following program demonstrates this:

Example #

using System;
namespace RectangleApplication
{
   class Rectangle
   {
      // Member variables
      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

1.35.3. C # multiple inheritance #

Multiple inheritance refers to the function that a category can inherit behaviors and features from more than one parent class at the same time. In contrast to single inheritance, single inheritance means that a class can inherit from only one parent class.

C# does not support multiple inheritance. However, you can use interfaces toimplement multiple inheritance. The following program demonstrates this:

Example #

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;
   }
   // Base class PaintCost
   public interface PaintCost
   {
      int getCost(int area);
   }
   // Derived class
   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();
         // Print the area of an 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

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.