• c#继承、多重继承


    c#类

    1、c#类的继承

    在现有类(基类、父类)上建立新类(派生类、子类)的处理过程称为继承。派生类能自动获得基类的除了构造函数和析构函数以外的所有成员,可以在派生类中添加新的属性和方法扩展其功能。继承的特性: 可传递性:C从B派生,B从A派生,那么C不仅继承B也继承A。 单一性:只能从一个基类中继承,不能同时继承多个基类继承中的访问修饰符base和this关键字基类的构造函数和析构函数不能被继承的。但可以使用关键字base来继承基类的构造函数。 C#中的base关键字代表基类,使用base关键字可以调用基类的构造函数、属性和方法。

    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

    2、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
  • 相关阅读:
    当数据库遇上外键
    java EE实现动态SQL的
    Java EE注册三部曲(一步曲)
    xml+js+html的二级联动
    MySQL中like的使用方法
    oracle 外部表查alter日志
    oracle 预安装命令
    LINUX 安装增强 前置安装文件
    linux 6.5 网卡
    liunux 6.5设置网卡默认开启
  • 原文地址:https://www.cnblogs.com/wys000/p/9585893.html
Copyright © 2020-2023  润新知