2
D语言有'别名...'。 Go嵌入了字段。在下面的代码中,Dart是否有办法到达啮齿动物的大肠,而不经过内脏?理想情况下,某种暴露内部组合的方法,不需要在每个动物中使用一些常见的内部组件创建转发呼叫?自动呼叫转移 - 访问啮齿动物的大肠
import 'dart:io';
class Stomach {
work() { print("stomach"); }
}
class LargeIntestine {
work() { print("large intestine"); }
}
class SmallIntestine {
work() { print("small intestine"); }
}
class Guts {
Stomach stomach = new Stomach();
LargeIntestine largeIntestine = new LargeIntestine();
SmallIntestine smallIntestine = new SmallIntestine();
work() {
stomach.work();
largeIntestine.work();
smallIntestine.work();
}
}
class Rodent {
Guts guts = new Guts();
work() => guts.work();
}
main() {
Rodent rodent = new Rodent();
rodent.guts.largeIntestine;
rodent.work();
}
谢谢。实际上,我正在寻找比为每个动物的每个器官宣布“获得”更少的工作。例如,用D你可以在Rodent中“混淆这个胆量”,然后你可以直接从Rodent中使用胆量成员。因此,这是一个玩具的例子,但假设将有30只动物,我不想要30 * 3的复制/粘贴获得者(即没有复制/粘贴转发)。我还没有在Dart中使用mixins,所以也许这有一个潜在的解决方案? – user1338952 2013-04-09 19:51:30