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. 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: Suppose there is a base class When the above code is compiled and executed, it produces the following results: 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: When the above code is compiled and executed, it produces the following results: 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: When the above code is compiled and executed, it produces the following results:
最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。 1.35.1. Base and derived classes #
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(); } } }
total area: 35
1.35.2. Initialization of base class #
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(); } } }
Length: 4.5 Width: 7.5 Area: 33.75 Cost: 2362.5
1.35.3. C # multiple inheritance #
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(); } } }
Total area: 35 Total Paint Cost: $2450