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();
}
}
}
继承相关关键字
base:访问基类成员(构造函数、方法、字段)virtual:标记方法可被重写override:重写基类虚方法sealed:密封类/方法,禁止继承/重写abstract:抽象类/方法,强制派生类实现
C语言
- C# 类与对象:面向对象编程的基石
- C# static 关键字:静态变量、方法与类完全掌握
- 理解 C# 嵌套类:定义、访问与继承实战
- C++ 类模板实战指南:编写灵活的类型无关代码
- C# 程序结构解析:从 Hello World 看命名空间、类、Main 入口与编译运行
- C# 基础语法与面向对象核心:类、成员、实例化、标识符与关键字
- C# 封装详解:访问修饰符与面向对象设计最佳实践
- 精通 Python 继承:概念、语法与实战示例
- 精通 Java 匿名内部类:定义、语法与实战示例
- 掌握 Java ObjectOutputStream:序列化、核心方法与实战示例
- 掌握 Java 泛型:构建可复用、类型安全的代码
- 掌握 Java 文件操作:java.io 包下的创建、读取、写入与删除