深入分析c# 继承

作者:菜鸟教程 时间:2023-02-18 21:36:31 

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

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

继承的思想实现了 属于(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
 {
  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("总面积: {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;
  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("成本: {0}", GetCost());
  }
 }
 class ExecuteRectangle
 {
  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

来源:https://www.runoob.com/csharp/csharp-inheritance.html

标签:c#,继承
0
投稿

猜你喜欢

  • java实现电话本系统

    2021-06-05 20:39:39
  • 基于Spring整合mybatis注解扫描是否成功的问题

    2023-01-23 18:29:22
  • 详解SpringBoot 快速整合Mybatis(去XML化+注解进阶)

    2022-02-19 03:54:29
  • Spring Boot2.0 @ConfigurationProperties使用详解

    2021-06-11 17:21:30
  • Java实现蓝桥杯G将军的示例代码

    2022-05-01 08:44:58
  • Android ToggleButton 详解及实例代码

    2022-09-11 03:58:02
  • C# .NET中Socket简单实用框架的使用教程

    2023-09-16 07:59:19
  • c#调用c++的DLL的实现方法

    2023-10-27 05:27:40
  • Android发送xml数据给服务器的方法

    2021-12-19 13:06:44
  • MyBatis超详细讲解如何实现分页功能

    2023-08-22 23:06:51
  • Java Swing实现简单的体重指数(BMI)计算器功能示例

    2022-12-16 09:34:56
  • SpringMVC @GetMapping注解路径冲突问题解决

    2022-12-18 01:43:17
  • Android PopupWindow 点击外面取消实现代码

    2022-02-04 20:52:05
  • c# 类成员的可访问性代码详解

    2022-03-14 22:43:59
  • Java 入门图形用户界面设计之单选按钮

    2023-08-29 13:40:02
  • C#对WPF数据绑定的菜单插入Seperator分隔

    2022-04-29 17:09:28
  • C#、vb.net及SQL判断指定年份是否为闰年的方法

    2023-05-18 09:49:55
  • java动态绑定和静态绑定用法实例详解

    2022-04-17 19:55:20
  • Android 高版本API方法在低版本系统上的兼容性处理

    2022-08-22 08:51:53
  • Kotlin四大组件中的broadcast广播

    2023-07-08 00:44:08
  • asp之家 软件编程 m.aspxhome.com