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");
}
//干活.