25用d编程对象

每个类自带toString opEquals opCmp toHash函数.因为继承自对象类.
定义他们,需要.因为有hash,所以可以成为关联数组的键.
TypeInfo提供类型的信息.每个类型一个.typeid可以访问相关信息.ti可用来比较是否相同,访问特殊函数,决定两个类型是否相同其经常关于运行时类型`.

class MusicalInstrument {
}

class StringInstrument : MusicalInstrument {
}

class Violin : StringInstrument {
}

class Guitar : StringInstrument {
}

void main() {
    TypeInfo v = typeid(Violin);
    TypeInfo g = typeid(Guitar);
    assert(v != g);    // 不一样
}
import std.stdio;

// ...

void foo(MusicalInstrument m, StringInstrument s) {
    const isSame = (typeid(m) == typeid(s));

    writefln("The types of the arguments are %s.",
             isSame ? "the same" : "different");
}

// ...

    auto a = new Violin();
    auto b = new Violin();
    foo(a, b);//相同的

.sizeof和typeof不执行表达式,而typeid要执行.

import std.stdio;

int foo(string when) {
    writefln("Called during '%s'.", when);
    return 0;
}

void main() {
    const s = foo("sizeof").sizeof; //未调用foo()
//不知道返回类型.
    alias T = typeof(foo("typeof"));//未调用foo()
    auto ti = typeid(foo("typeid"));//调用foo()
//即要调用表达式.
//结果:Called during 'typeid'.
//如
}
MusicalInstrument foo(int i) {
    return (i % 2) ? new Violin() : new Guitar();
}

   TypeInfo_Class info = a.classinfo;//访问对象的类信息,
    string runtimeTypeName = info.name;
//得到运行时类型串

arrays, structs, classes有各种各样typeinfo的子类.TypeInfo_Class就很有用.

    auto clock = new Clock(20, 30, 0);
    writeln(clock);     //调用 对象.到串.
//---
import std.string;

class Clock {
     盖 串 toString()const {
         返回格式("%02s:%02s:%02s",小时,分钟,);
    }

    // ...
 }

class AlarmClock:Clock {
     override string toString()const {
         return format("%s%02s:%02s",super .toString(),alarmHour,alarmMinute);
    }

    // ...
 }

// ...
 
    auto bedSideClock = new AlarmClock(20,30,0,7,0 );
    writeln(bedSideClock);

//两个类成员`==`四步走
bool opEquals(Object a, Object b) {
    if (a is b) return true;                          // (1)
    if (a is null || b is null) return false;         // (2)
    if (typeid(a) == typeid(b)) return a.opEquals(b); // (3)
    return a.opEquals(b) && b.opEquals(a);            // (4)
}//两对象相同.

opCmpopEquals必须一致.

 auto variable0 = new Clock(6, 7, 8);
    auto variable1 = new Clock(6, 7, 8);

    assert(variable0 != variable1); 
//是否指向相同实体,否,则不等.
    auto partner0 = new Clock(9, 10, 11);
    auto partner1 = partner0;

    assert(partner0 == partner1);
//相同实体.

按值比较是否相同:

class Clock {
    override bool opEquals(Object o) const {
        // ...
    }//类的参数类型为`对象`,o表`==`右边的对象
    auto rhs = cast(const Clock)o;//第一步,转换类型,而且是常类型,不能修改的右边对象
    // ...
}
//
class Clock {
    int hour;
    int minute;
    int second;

    override bool opEquals(Object o) const {
        auto rhs = cast(const Clock)o;
        return (rhs &&
                (hour == rhs.hour) &&
                (minute == rhs.minute) &&
                (second == rhs.second));
    }//按值比较

    // ...
}

别忘记了父

class AlarmClock : Clock {
    int alarmHour;
    int alarmMinute;

    override bool opEquals(Object o) const {
        auto rhs = cast(const AlarmClock)o;

        return (rhs &&
                (alarmHour == rhs.alarmHour) &&
                (alarmMinute == rhs.alarmMinute) &&
                super.opEquals(o));
                //最好不要super==o,不然又是四步走,要慢一些
    }

    // ...
}

opCmp,排序时常用
关联数组要定义opEquals和opCmp,toHash,三个.

发布了405 篇原创文章 · 获赞 25 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/fqbqrr/article/details/104588217
今日推荐