Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
19
Task/Polymorphic-copy/D/polymorphic-copy-1.d
Normal file
19
Task/Polymorphic-copy/D/polymorphic-copy-1.d
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
class T {
|
||||
override string toString() { return "I'm the instance of T"; }
|
||||
T duplicate() { return new T; }
|
||||
}
|
||||
|
||||
class S : T {
|
||||
override string toString() { return "I'm the instance of S"; }
|
||||
|
||||
override T duplicate() { return new S; }
|
||||
}
|
||||
|
||||
void main () {
|
||||
import std.stdio;
|
||||
T orig = new S;
|
||||
T copy = orig.duplicate();
|
||||
|
||||
writeln(orig);
|
||||
writeln(copy);
|
||||
}
|
||||
46
Task/Polymorphic-copy/D/polymorphic-copy-2.d
Normal file
46
Task/Polymorphic-copy/D/polymorphic-copy-2.d
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
class T {
|
||||
this(T t = null) {} // Constructor that will be used for copying.
|
||||
|
||||
override string toString() { return "I'm the instance of T"; }
|
||||
|
||||
T duplicate() { return new T(this); }
|
||||
|
||||
bool custom(char c) { return false; }
|
||||
}
|
||||
|
||||
class S : T {
|
||||
char[] str;
|
||||
|
||||
this(S s = null) {
|
||||
super(s);
|
||||
if (s is null)
|
||||
str = ['1', '2', '3']; // All newly created will get that.
|
||||
else
|
||||
str = s.str.dup; // Do the deep-copy.
|
||||
}
|
||||
|
||||
override string toString() {
|
||||
return "I'm the instance of S p: " ~ str.idup;
|
||||
}
|
||||
|
||||
override T duplicate() { return new S(this); }
|
||||
|
||||
// Additional procedure, just to test deep-copy.
|
||||
override bool custom(char c) {
|
||||
if (str !is null)
|
||||
str[0] = c;
|
||||
return str is null;
|
||||
}
|
||||
}
|
||||
|
||||
void main () {
|
||||
import std.stdio;
|
||||
T orig = new S;
|
||||
orig.custom('X');
|
||||
|
||||
T copy = orig.duplicate();
|
||||
orig.custom('Y');
|
||||
|
||||
orig.writeln;
|
||||
copy.writeln; // Should have 'X' at the beginning.
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue