C# 中的基础类和派生类是什么?
一个类可以从多个类或接口中派生得出,这意味着它可以从多个基类或接口中继承数据和函数。
例如,带有如下派生类的车辆基类。
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;
}
// Derived class
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);
// Print the area of the object.
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.ReadKey();
}
}
}输出
Total area: 35
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP