C# 继承

Feb 08, 2017 pm 01:35 PM
c# 继承

继承是面向对象程序设计中最重要的概念之一。继承允许我们根据一个类来定义另一个类来定义一个类,这使得创建和维护应用程序变得更容易。同时也有利于重用代码和节省开发时间。

当创建一个类时,程序员不需要完全重新编写新的数据成员和成员函数,只需要设计一个新的类,继承了已有的类的成员即可。这个已有的类被称为的基类,这个新的类被称为派生类。

继承的思想实现了 属于(IS-A) 关系。例如,哺乳动物 属于(IS-A) 动物,狗 属于(IS-A) 哺乳动物,因此狗 属于(IS-A) 动物。

基类和派生类

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

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

 class { ... } 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   {      publicint getArea()      {         return (width * height);      }   }     class RectangleTester   {      static voidMain(string[] args)      {         Rectangle Rect = new Rectangle();         Rect.setWidth(5);         Rect.setHeight(7);         // 打印对象的面积         Console.WriteLine("总面积: {0}",  Rect.getArea());         Console.ReadKey();      }   }}

当上面的代码被编译和执行时,它会产生下列结果:

总面积: 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("长度: {0}", length);         Console.WriteLine("宽度: {0}", width);         Console.WriteLine("面积: {0}", GetArea());      }   }//end class Rectangle     class Tabletop : Rectangle   {      private double cost;      publicTabletop(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());      }   }   classExecuteRectangle   {      static void Main(string[] args)      {         Tabletop t = new Tabletop(4.5,7.5);         t.Display();         Console.ReadLine();      }   }}

当上面的代码被编译和执行时,它会产生下列结果:

长度: 4.5宽度: 7.5面积: 33.75成本: 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("总面积: {0}",  Rect.getArea());         Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area));         Console.ReadKey();      }   }}

当上面的代码被编译和执行时,它会产生下列结果:

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

更多C# 继承相关文章请关注PHP中文网!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

使用 C# 的活动目录 使用 C# 的活动目录 Sep 03, 2024 pm 03:33 PM

使用 C# 的 Active Directory 指南。在这里,我们讨论 Active Directory 在 C# 中的介绍和工作原理以及语法和示例。

C# 序列化 C# 序列化 Sep 03, 2024 pm 03:30 PM

C# 序列化指南。这里我们分别讨论C#序列化对象的介绍、步骤、工作原理和示例。

C# 中的随机数生成器 C# 中的随机数生成器 Sep 03, 2024 pm 03:34 PM

C# 随机数生成器指南。在这里,我们讨论随机数生成器的工作原理、伪随机数和安全数的概念。

C# 数据网格视图 C# 数据网格视图 Sep 03, 2024 pm 03:32 PM

C# 数据网格视图指南。在这里,我们讨论如何从 SQL 数据库或 Excel 文件加载和导出数据网格视图的示例。

C# 中的模式 C# 中的模式 Sep 03, 2024 pm 03:33 PM

C# 模式指南。在这里,我们讨论 C# 中模式的介绍和前 3 种类型,以及其示例和代码实现。

C# 中的质数 C# 中的质数 Sep 03, 2024 pm 03:35 PM

C# 素数指南。这里我们讨论c#中素数的介绍和示例以及代码实现。

C# 中的阶乘 C# 中的阶乘 Sep 03, 2024 pm 03:34 PM

C# 阶乘指南。这里我们讨论 C# 中阶乘的介绍以及不同的示例和代码实现。

c#多线程和异步的区别 c#多线程和异步的区别 Apr 03, 2025 pm 02:57 PM

多线程和异步的区别在于,多线程同时执行多个线程,而异步在不阻塞当前线程的情况下执行操作。多线程用于计算密集型任务,而异步用于用户交互操作。多线程的优势是提高计算性能,异步的优势是不阻塞 UI 线程。选择多线程还是异步取决于任务性质:计算密集型任务使用多线程,与外部资源交互且需要保持 UI 响应的任务使用异步。

See all articles