【发布时间】:2013-12-06 04:22:51
【问题描述】:
所以,从 Ruby 开始,做这样的事情是一种常见的模式:
class Animal
class << self
def type(*args)
if args.length == 1
@type = type.first
else
@type
end
end
end
end
class Dog < Animal
type "Mammal"
end
Dog.type # => "Mammal"
但我无法让类似的东西在 D 中工作:
class Animal {
static string animalType;
static void type(string type) {
animalType = type;
}
}
class Dog : Animal {
type(cast(string)"Mammal");
}
我得到编译器错误:
Error: unexpected ( in declarator
Error: basic type expected, not cast
Error: found 'cast' when expecting ')'
Error: no identifier for declarator type(int)
Error: semicolon expected following function declaration
Error: Declaration expected, not '('
这可能吗?
【问题讨论】:
-
您确定您的 Ruby 代码有效吗?我喜欢 Ruby,但你不必从 Animal 继承吗?换句话说,我认为您的代码中需要
class Dog < Animal。对吗? -
你是对的,我的 Ruby 中有一个错误。
标签: d