24用d编程继承
class SubClass : SuperClass {
// ...
}
class Clock {
int hour;
int minute;
int second;
void adjust(int hour, int minute, int second = 0) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
}
class AlarmClock : Clock {
int alarmHour;
int alarmMinute;
void adjustAlarm(int hour, int minute) {
alarmHour = hour;
alarmMinute = minute;
}
}
class Clock {
Battery battery; //有一个
// ...
}
//类不能继承多个非接口类,
class AlarmClock : Clock {
// ...
void foo() {
super.minute = 10; //父
minute = 10; //无歧义的话,同上
}
}
//----
class Device {
string manufacturer;
}
class Clock : Device {
string manufacturer;
}
class AlarmClock : Clock {
// ...
void foo() {
Device.manufacturer = "Sunny Horology, Inc.";
Clock.manufacturer = "Better Watches, Ltd.";
//加个类名,消除歧义
}
}
//---
class AlarmClock : Clock {
this(int hour, int minute, int second, // for Clock's members
int alarmHour, int alarmMinute) { // for AlarmClock's members
super(hour, minute, second);//调用父类
this.alarmHour = alarmHour;
this.alarmMinute = alarmMinute;
}
// ...
}
//多态
class Clock {
void reset() {
hour = 0;
minute = 0;
second = 0;
}
// ...
}
class AlarmClock : Clock {
override void reset() {
super.reset();
alarmHour = 0;
alarmMinute = 0;
}//继承
// ...
}
void use(Clock clock) {
// ...
clock.reset();//父类作接口
// ...
}
auto deskClock = new AlarmClock(10, 15, 0, 6, 45);
writeln("Before: ", deskClock);
use(deskClock);
writeln("After : ", deskClock);//子类变化.多态
//---
import std.random;
class BrokenClock : Clock {
this() {
super(0, 0, 0);
}
override void reset() {
hour = uniform(0, 24);
minute = uniform(0, 60);
second = uniform(0, 60);
}
}//重置.
auto shelfClock = new BrokenClock;
use(shelfClock);
writeln(shelfClock);
//是不是变了.仍然按子类的.
//抽象成员函数
class ChessPiece {
abstract bool isValid(in Square from, in Square to);
}//必须实现抽象成员函数.父类可能有实现
class Pawn : ChessPiece {
override bool isValid(in Square from, in Square to) {
return decision;
}//自己实现
}
class ChessPiece {
abstract bool isValid(in Square from, in Square to) {
return from != to;
}//父抽象成员函数有实现
}
class Pawn : ChessPiece {
override bool isValid(in Square from, in Square to) {
if (!super.isValid(from, to)) {//先调用父
return false;
}//检查是父?
//检查子类
return decision;
}
}
示例:
class RailwayVehicle {
void advance(in size_t kilometers) {
writefln("The vehicle is advancing %s kilometers",kilometers);
}
}
class Locomotive : RailwayVehicle {
}
class RailwayCar : RailwayVehicle {
abstract void load();
abstract void unload();
}//接口
class PassengerCar : RailwayCar {
override void load() {
writeln("The passengers are getting on");
}
override void unload() {
writeln("The passengers are getting off");
}
}
class FreightCar : RailwayCar {
override void load() {
writeln("The crates are being loaded");
}
override void unload() {
writeln("The crates are being unloaded");
}
}
//两个继承类
class Train : RailwayVehicle {
Locomotive locomotive;
RailwayCar[] cars;
// ...
}//铁路
import std.exception;
// ...
class Train : RailwayVehicle {
// ...
this(Locomotive locomotive) {
enforce(locomotive !is null,
"Locomotive cannot be null");
this.locomotive = locomotive;
}
void addCar(RailwayCar[] cars...) {
this.cars ~= cars;
}//加车子
// ...
}
class Train : RailwayVehicle {
// ...
void departStation(string station) {
foreach (car; cars) {
car.load();
}
writefln("Departing from %s station", station);
}
void arriveStation(string station) {
writefln("Arriving at %s station", station);
foreach (car; cars) {
car.unload();
}
}
}
//加站点
import std.stdio;
// ...
void main() {
auto locomotive = new Locomotive;
auto train = new Train(locomotive);
train.addCar(new PassengerCar, new FreightCar);
train.departStation("Ankara");
train.advance(500);
train.arriveStation("Haydarpa?a");
}
//干活.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现