D: 如何在 \abstract class 中创建具有任意参数的所需方法?
D: How to create a required method with arbitrary parameters in \abstract class?
假设我有一个狗的摘要class,定义如下:
abstract class Dog {
public:
uint age;
abstract void moveDog();
abstract void feedDog();
abstract void playFetch();
}
现在我想创建两个不同的 classes 来扩展 Dog,但是 moveDog、feedDog 和 playFetch 方法每个都有不同的实现和不同的参数,例如这两个:
class BigDog: Dog {
public:
override void moveDog(uint steps) {...}
override void feedDog(uint numFoodBowls) {...}
override void playFetch(uint timeToPlay, float throwDistance) {...}
}
class SmallDog: Dog {
public:
override void moveDog(uint steps, float direction) {...}
override void feedDog(float foodWeight) {...}
override void playFetch(float throwStrength, uint timeToPlay, float throwDistance) {...}
}
这在 D 中可行吗?我只是想强制 Dog 的所有子classes 实现三个具有相同名称的方法,但我不想指定所述方法的参数和实现。
你能达到你想要的,并且仍然有一定意义的唯一方法是,如果你能想出所有可能的(通用)参数,并使用默认值。
我的想法是这样的:
import std.stdio;
abstract class Dog {
public:
uint age;
abstract void moveDog(uint steps, float direction=float.nan);
abstract void feedDog(uint numFoodBowls=-1, float foodWeight=float.nan);
abstract void playFetch(uint timeToPlay, float throwDistance, float throwStrength=float.nan);
}
class BigDog: Dog {
public:
override void moveDog(uint steps, float direction) {
writeln(direction);
}
override void feedDog(uint numFoodBowls=-1, float foodWeight=float.nan) {}
override void playFetch(uint timeToPlay, float throwDistance, float throwStrength=float.nan) {}
}
class SmallDog: Dog {
public:
override void moveDog(uint steps, float direction) {
writeln(direction);
}
override void feedDog(uint numFoodBowls=-1, float foodWeight=float.nan) {}
override void playFetch(uint timeToPlay, float throwDistance, float throwStrength) {}
}
void main() {
auto obj = new BigDog();
obj.moveDog(5, -0.1f); // -0.1
}
最后,正如其他人指出的那样,如果您的 "behaviour"(方法定义行为)对于 Dog 的每个 "kind"(subclass)都是不同的,那么它不应该在界面(抽象class)...
假设我有一个狗的摘要class,定义如下:
abstract class Dog {
public:
uint age;
abstract void moveDog();
abstract void feedDog();
abstract void playFetch();
}
现在我想创建两个不同的 classes 来扩展 Dog,但是 moveDog、feedDog 和 playFetch 方法每个都有不同的实现和不同的参数,例如这两个:
class BigDog: Dog {
public:
override void moveDog(uint steps) {...}
override void feedDog(uint numFoodBowls) {...}
override void playFetch(uint timeToPlay, float throwDistance) {...}
}
class SmallDog: Dog {
public:
override void moveDog(uint steps, float direction) {...}
override void feedDog(float foodWeight) {...}
override void playFetch(float throwStrength, uint timeToPlay, float throwDistance) {...}
}
这在 D 中可行吗?我只是想强制 Dog 的所有子classes 实现三个具有相同名称的方法,但我不想指定所述方法的参数和实现。
你能达到你想要的,并且仍然有一定意义的唯一方法是,如果你能想出所有可能的(通用)参数,并使用默认值。
我的想法是这样的:
import std.stdio;
abstract class Dog {
public:
uint age;
abstract void moveDog(uint steps, float direction=float.nan);
abstract void feedDog(uint numFoodBowls=-1, float foodWeight=float.nan);
abstract void playFetch(uint timeToPlay, float throwDistance, float throwStrength=float.nan);
}
class BigDog: Dog {
public:
override void moveDog(uint steps, float direction) {
writeln(direction);
}
override void feedDog(uint numFoodBowls=-1, float foodWeight=float.nan) {}
override void playFetch(uint timeToPlay, float throwDistance, float throwStrength=float.nan) {}
}
class SmallDog: Dog {
public:
override void moveDog(uint steps, float direction) {
writeln(direction);
}
override void feedDog(uint numFoodBowls=-1, float foodWeight=float.nan) {}
override void playFetch(uint timeToPlay, float throwDistance, float throwStrength) {}
}
void main() {
auto obj = new BigDog();
obj.moveDog(5, -0.1f); // -0.1
}
最后,正如其他人指出的那样,如果您的 "behaviour"(方法定义行为)对于 Dog 的每个 "kind"(subclass)都是不同的,那么它不应该在界面(抽象class)...