返回 D 中类名的函数
比如说,类 A1、A2、...、An 都扩展了抽象类 B。 我希望 A1,...,An 有一个返回类名字符串的函数。 这在编译时当然是已知的,但我想在 B,并使用继承,以便所有 Ai:s 都获得此功能。
在java中,这可以很容易地完成,只要让B
String getName() {
return this.getClass();
}
或多或少地拥有该方法即可。那么,我该如何在 D 中做到这一点呢?另外,有没有办法使用特征或类似的方法来确定哪些类成员是公共的?
Say, classes A1,A2,...,An all extends the abstract class B.
I would like A1,...,An to have a function that returns a string of the class name.
This is certainly known in compile-time, but I would like to implement this function in
B, and use inheritance so that all Ai:s get this functionality.
In java, this can easily be done, by letting B have the method
String getName() {
return this.getClass();
}
more or less. So, how do I do this in D? Also, is there a way, using traits or similar, to determine which class members are public?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只是
typeof(this).stringof
但是这是在编译时修复的,因此继承不会更改该值
将给出实例的类名的动态名称
http://www.d-programming-language.org/Expression.html#typeidExpression
http://www.d-programming-language.org/phobos/object .html#TypeInfo_Class
simply
typeof(this).stringof
however this is fixed at compile time so inheritance doesn't change the value
will give the dynamic name of the classname of the instance
http://www.d-programming-language.org/expression.html#typeidexpression
http://www.d-programming-language.org/phobos/object.html#TypeInfo_Class
它在编译时是已知的,但我认为在运行时评估类名需要进行分解。
这是运行时评估是否正常的情况:
It is known at compile-time, but evaluating the class name at runtime requires demangling, I think.
Here it is if runtime-evaluation is okay:
您只需使用
ClassName.stringof
即可获取类的名称。如果您希望它作为虚拟函数,那么我建议使用奇怪的重复模板模式:
不幸的是,目前无法确定哪些类成员是公开的。您可以使用 allMembers 特征 迭代所有成员。
You can get the name of a class simply by using
ClassName.stringof
.If you want it as a virtual function then I would recommend using the Curiously Recurring Template Pattern:
Unfortunately, at the moment there is no way to determine which class members are public. You can iterate all members by using the allMembers trait.
这对我有用 - 假设运行时评估没问题,并且您只对实际的类 name 感兴趣,而不对包路径或模块名称感兴趣。
Here is what works for me - assuming runtime-evaluation is okay, and you're only interested in the actual class name without the package-path or the module name.