自学内容网 自学内容网

C# 中继承的说明

一.继承的相关概念说明

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

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

二.基类和派生类

一个类可以继承自另一个类,也就是基类(父类)和派生类(子类)。C# 不支持类的多重继承,但支持接口的多重继承,一个类可以实现多个接口。

概括来说:一个类可以继承多个接口,但只能继承自一个类,创建派生类的语法如下:

 <访问修饰符> class <基类>
{
 ...
}
class <派生类> : <基类>
{
 ...
}

 注:派生类会继承基类的成员(字段、方法、属性等),除非它们被明确地标记为私有(private)。

派生类可以通过关键字base来调用基类的构造函数和方法。

class BaseClass
{
    public void SomeMethod()
    {
        // 执行方法代码块
    }
}

class DerivedClass : BaseClass
{
    public void AnotherMethod()
    {
        // 执行方法代码块
        base.SomeMethod();       
    }
}

三.基类的初始化

派生类继承了基类的成员变量和成员方法,因此父类对象会在子类对象创建之前被创建,也可以在成员初始化列表中进行父类的初始化。

   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(0.5, 0.7);
         t.Display();
         Console.ReadLine();
      }
   }

四.继承接口(Interface Inheritance)

一个接口可以继承自一个或多个其他接口,派生接口继承了基接口的所有成员,派生接口可以扩展基接口的成员列表,但不能改变它们的访问修饰符。

interface IBaseInterface
{
    void Method1();
}

interface IDerivedInterface : IBaseInterface
{
    void Method2();
}

五.C# 中的多重继承

多重继承指的是一个类别可以同时从多于一个父类继承行为与特征的功能,与单一继承相对,单一继承指一个类别只可以继承自一个父类。C# 不支持多重继承,但是可以使用接口来实现多重继承。

 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 * 10;
      }
   }
   class RectangleTester
   {
      static void Main(string[] args)
      {
         Rectangle Rect = new Rectangle();
         int area;
         Rect.setWidth(10);
         Rect.setHeight(15);
         area = Rect.getArea();
         // 打印对象的面积
         Console.WriteLine("总面积: {0}",  Rect.getArea());
         Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area));
         Console.ReadKey();
      }
   }

 


原文地址:https://blog.csdn.net/weixin_53520014/article/details/142753371

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!