check type
bearophile
bearophileHUGS at lycos.com
Sat Nov 6 17:50:05 PDT 2010
spir:
> class SC {
> string f = "SC";
> void show() {writeln(this.f);}
> }
> class C : SC {
> string f = "C";
> }
> void main () {
> auto c = new C();
> writeln(c.f) ; // OK, got "C"
> c.show() ; // expected "C", got "SC"
>
> // below test for type
> if (is(typeof(c) == C)) {writeln("type C");} else {writeln("type SC");}
> auto sc = new SC();
> if (is(typeof(sc) == C)) {writeln("type C");} else {writeln("type SC");}
> }
Java acts as D here:
http://ideone.com/LcAll
C# requires the new keyword:
http://ideone.com/AVFvI
A D version that does as you desire:
import std.stdio;
class SC {
string f_ = "SC";
@property string f() { return f_; }
void show() { writeln(this.f); }
}
class C : SC {
string f_ = "C";
@property override string f() { return f_; }
}
void main () {
auto c = new C();
writeln(c.classinfo.name);
auto sc = new SC();
writeln(sc.classinfo.name);
writeln(c.f); // "C"
c.show(); // "C"
}
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list