主页
  • 主页
  • 分类
  • 热文
  • 教程
  • 面试
  • 标签
C#

C# 基础

C# 主页
C# 概述
C# 环境
C# 程序结构
C# 基本语法
C# 数据类型
C# 类型转换
C# 变量
C# 常量
C# 运算符
C# 决策结构
C# 循环结构
C# 封装
C# 方法
C# 可空类型
C# 数组
C# 字符串
C# 结构体
C# 枚举
C# 类
C# 继承
C# 多态性
C# 操作符重载
C# 接口
C# 命名空间
C# 预处理
C# 正则表达式
C# 异常
C# 文件与流

C# 高级

C# 属性(Attributes)
C# 反射
C# 属性(Properties)
C# 索引器
C# 委托
C# 事件
C# 集合
C# 泛型
C# 匿名方法
C# 不安全代码
C# 线程

基础

C# 主页
C# 概述
C# 环境
C# 程序结构
C# 基本语法
C# 数据类型
C# 类型转换
C# 变量
C# 常量
C# 运算符
C# 决策结构
C# 循环结构
C# 封装
C# 方法
C# 可空类型
C# 数组
C# 字符串
C# 结构体
C# 枚举
C# 类
C# 继承
C# 多态性
C# 操作符重载
C# 接口
C# 命名空间
C# 预处理
C# 正则表达式
C# 异常
C# 文件与流

高级

C# 属性(Attributes)
C# 反射
C# 属性(Properties)
C# 索引器
C# 委托
C# 事件
C# 集合
C# 泛型
C# 匿名方法
C# 不安全代码
C# 线程

C# 继承


上一章 下一章

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

在创建一个类时,而不是完全编写新的数据成员和成员函数,程序员可以指定新类应该继承现有类的成员。这个现有的类被称为基类,而新类则被称为派生类。

继承的概念实现了IS-A关系。例如,哺乳动物是一种动物,狗是一种哺乳动物,因此狗也是一种动物,等等。

基类和派生类

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

C# 中创建派生类的语法如下:

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

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

考虑一个基类 Shape 和其派生类 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("Total area: {0}",  Rect.getArea());
         Console.ReadKey();
      }
   }
}

当上述代码被编译和执行时,会产生如下结果:

Total area: 35

初始化基类

派生类继承了基类的成员变量和成员方法。因此,在创建子类之前应该先创建超类的对象。你可以在成员初始化列表中给出超类初始化的指令。

下面的程序演示了这一点:

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("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

C# 中的多重继承

C# 不支持多重继承。但是,你可以使用接口来实现多重继承。下面的程序演示了这一点:

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("Total area: {0}",  Rect.getArea());
         Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }
}

当上述代码被编译和执行时,会产生如下结果:

Total area: 35
Total paint cost: $2450
上一章 下一章
阅读号二维码

关注阅读号

联系二维码

联系我们

© 2024 Yoagoa. All rights reserved.

粤ICP备18007391号

站点地图