如何将一个类传递给另一个类方法?使用这个例子,“animal.name”在 eat 方法中将是空的。
import std.stdio;
void main()
{
Animal cat = new Animal;
Animal dog = new Animal;
cat.name = "fluffy";
dog.name = "bruce";
dog.eat(cat);
}
class Animal
{
auto name = "none";
void eat( Animal animal )
{
writeln(this.name);
writeln("is going to eat");
writeln(animal.name);
}
}