什么是 C# 中的基类和派生类?
一个类可以派生自多个类或接口,这意味着它可以从多个基类或接口继承数据和函数。
例如,带有以下派生类的VehicleBase类。
Truck Bus Motobike
派生类继承了基类的成员变量和成员方法。
同样,Shape类的派生类可以是Rectangle,如下例所示。
示例
using System;
namespace Program {
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 Demo {
static void Main(string[] args) {
Rectangle Rect = new Rectangle();
Rect.setWidth(5);
Rect.setHeight(7);
//打印对象的面积。
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.ReadKey();
}
}
}输出结果Total area: 35
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下