C#继承

C#继承 首页 / C#入门教程 / C#继承

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

基类和继承类

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

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;
   }

   //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();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-inheritance.html

来源:LearnFk无涯教程网

Total area: 35

初始化基类

继承类继承基类变量和方法,因此,应该在创建子类之前创建超类对象,您可以在初始化列表中提供超类初始化的说明。

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();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-inheritance.html

来源:LearnFk无涯教程网

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

多重继承

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;
   }

   //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();
      }
   }
}

编译并执行上述代码时,将生成以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-inheritance.html

来源:LearnFk无涯教程网

Total area: 35
Total paint cost: $2450

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

Go 语言项目开发实战 -〔孔令飞〕

爆款文案修炼手册 -〔乐剑峰〕

陶辉的网络协议集训班02期 -〔陶辉〕

物联网开发实战 -〔郭朝斌〕

即时消息技术剖析与实战 -〔袁武林〕

玩转Git三剑客 -〔苏玲〕

白话法律42讲 -〔周甲徳〕

深入浅出区块链 -〔陈浩〕

左耳听风 -〔陈皓〕

好记忆不如烂笔头。留下您的足迹吧 :)