Csharp 简明教程

C

面向对象编程中最重要概念之一便是继承。继承允许我们根据另一个类定义类,这使得创建和维护应用程序变得更加容易。它还提供了重用代码功能的机会并加快了实现时间。

One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and speeds up implementation time.

在创建类时,编程人员可以指定新类应该继承现有类的成员,而不是编写全新的数据成员和成员函数。这个现有的类叫做 base 类,而新类被称为 derived 类。

When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.

继承思想实现了 IS-A 关系。例如,哺乳动物 IS A 动物,狗 IS-A 哺乳动物因而狗 IS-A 也动物,依此类推。

The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.

Base and Derived Classes

一个类可以从多个类或接口派生,这意味着它可以从多个基类或接口继承数据和功能。

A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces.

在 C# 中用于创建派生类所使用的语法如下 −

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

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

class <derived_class> : <base_class> {
   ...
}

考虑一个基类 Shape 和它的派生类 Rectangle −

Consider a base class Shape and its derived class 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;
   }

   // 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 the object.
         Console.WriteLine("Total area: {0}",  Rect.getArea());
         Console.ReadKey();
      }
   }
}

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Total area: 35

Initializing Base Class

派生类继承基类的成员变量和成员方法。因此,在构建子类之前应当构建超类对象。你可以在成员初始化列表中为超类初始化提供指示。

The derived class inherits the base class member variables and member methods. Therefore the super class object should be created before the subclass is created. You can give instructions for superclass initialization in the member initialization list.

以下程序演示了这一点 −

The following program demonstrates this −

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 result −

Length: 4.5
Width: 7.5
Area: 33.75
Cost: 2362.5

Multiple Inheritance in C

C# does not support 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;
   }

   // 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 the 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 result −

Total area: 35
Total paint cost: $2450