亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C# 继承完全指南:基类、派生类、初始化与多重继承替代方案

继承是面向对象编程最重要概念之一。继承允许以现有类为基础定义新类,便于创建和维护应用,实现代码复用,加快实现速度。

新类(派生类)继承现有类(基类)的成员。继承实现 IS-A 关系:哺乳动物 IS-A 动物,狗 IS-A 哺乳动物,因此狗 IS-A 动物。

基类与派生类

类可从多个类或接口派生,继承多个基类或接口的数据和函数。C# 创建派生类语法:

<access-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;
    }
    
    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("Total area: {0}", Rect.getArea());  // 35
            Console.ReadKey();
        }
    }
}

基类初始化

派生类继承基类成员,基类对象应在子类前创建。可在成员初始化列表指定基类初始化:

class Tabletop : Rectangle {
    private double cost;
    public Tabletop(double l, double w) : base(l, w) { }  // 调用基类构造函数
    public double GetCost() { return GetArea() * 70; }
    public void Display() {
        base.Display();  // 调用基类方法
        Console.WriteLine("Cost: {0}", GetCost());
    }
}

完整示例

using System;
namespace RectangleApplication {
    class Rectangle {
        protected double length, 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());
        }
    }
    
    class Tabletop : Rectangle {
        private double cost;
        public Tabletop(double l, double w) : base(l, w) { }
        public double GetCost() { return GetArea() * 70; }
        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();
        }
    }
}

输出:

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

C# 多重继承

C# 不支持多重继承(一个类不能直接继承多个类)。但可通过接口实现多重继承效果:

interface PaintCost { int getCost(int area); }

class Rectangle : Shape, PaintCost {
    public int getArea() { return width * height; }
    public int getCost(int area) { return area * 70; }
}

派生类用逗号分隔继承的基类和接口。接口只包含成员声明,实现由派生类提供。

完整示例

using System;
namespace InheritanceApplication {
    class Shape {
        public void setWidth(int w) { width = w; }
        public void setHeight(int h) { height = h; }
        protected int width, height;
    }
    
    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();
            Rect.setWidth(5); Rect.setHeight(7);
            int area = Rect.getArea();
            Console.WriteLine("Total area: {0}", area);           // 35
            Console.WriteLine("Total paint cost: ${0}", Rect.getCost(area));  // 2450
            Console.ReadKey();
        }
    }
}

继承相关关键字

C语言

  1. C# 类与对象:面向对象编程的基石
  2. C# static 关键字:静态变量、方法与类完全掌握
  3. 理解 C# 嵌套类:定义、访问与继承实战
  4. C++ 类模板实战指南:编写灵活的类型无关代码
  5. C# 程序结构解析:从 Hello World 看命名空间、类、Main 入口与编译运行
  6. C# 基础语法与面向对象核心:类、成员、实例化、标识符与关键字
  7. C# 封装详解:访问修饰符与面向对象设计最佳实践
  8. 精通 Python 继承:概念、语法与实战示例
  9. 精通 Java 匿名内部类:定义、语法与实战示例
  10. 掌握 Java ObjectOutputStream:序列化、核心方法与实战示例
  11. 掌握 Java 泛型:构建可复用、类型安全的代码
  12. 掌握 Java 文件操作:java.io 包下的创建、读取、写入与删除