June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
12
Task/Inheritance-Multiple/D/inheritance-multiple-2.d
Normal file
12
Task/Inheritance-Multiple/D/inheritance-multiple-2.d
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
interface Camera {
|
||||
// A virtual function.
|
||||
Image takePhoto();
|
||||
|
||||
// A non-virtual function.
|
||||
final Image[] takeSeveralPhotos(int count) {
|
||||
auto result = new Image[count];
|
||||
foreach (ref img; result) {
|
||||
img = takePhoto();
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Task/Inheritance-Multiple/D/inheritance-multiple-3.d
Normal file
38
Task/Inheritance-Multiple/D/inheritance-multiple-3.d
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
class A {
|
||||
string foo() {
|
||||
return "I am an A.";
|
||||
}
|
||||
}
|
||||
class B {
|
||||
string foo() {
|
||||
return "I am a B.";
|
||||
}
|
||||
}
|
||||
|
||||
class C : A {
|
||||
string className = "C";
|
||||
override string foo() {
|
||||
return "I am a "~className~", and thus an A.";
|
||||
}
|
||||
@property
|
||||
BWrapper asB() {
|
||||
return new BWrapper();
|
||||
}
|
||||
alias asB this;
|
||||
class BWrapper : B {
|
||||
override string foo() {
|
||||
return "I am a "~className~", disguised as a B.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unittest {
|
||||
import std.stdio : writeln;
|
||||
|
||||
auto c = new C();
|
||||
A a = c;
|
||||
B b = c;
|
||||
|
||||
writeln(a.foo());
|
||||
writeln(b.foo());
|
||||
}
|
||||
19
Task/Inheritance-Multiple/D/inheritance-multiple-4.d
Normal file
19
Task/Inheritance-Multiple/D/inheritance-multiple-4.d
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
template registerable() {
|
||||
void register() { /* implementation */ }
|
||||
}
|
||||
|
||||
string makeFunction(string s) {
|
||||
return `string `~s~`(){ return "`~s~`";}`;
|
||||
}
|
||||
|
||||
class Foo {
|
||||
mixin registerable!();
|
||||
mixin(makeFunction("myFunction"));
|
||||
}
|
||||
|
||||
unittest {
|
||||
import std.stdio : writeln;
|
||||
Foo foo = new Foo;
|
||||
foo.register();
|
||||
writeln(foo.myFunction());
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue