可继承的静态方法 [英] Inheritable Static methods
问题描述
有没有办法让这个方法可以继承并让它在继承的类上适当地运行?b $ b?使用泛型?扩展方法?
别的什么?
class baseClass
{
public static string ClassName( )
{
返回typeof(baseClass).FullName;
}
}
我想要的是:
class someClass:baseClass
{
.... <
}
并且能够调用someClass.ClassName()并让它返回
" someClass"
显然我可以在每个继承的
子类中手动重新实现ClassName(),但是我觉得应该有 - 添加的方式
a静态方法返回一个返回自己的类型名称的类,没有
必须为每个继承的子类手动执行。
-regards,
Dave
Hi,
Is there any way to make this method inheritable and have it behave
appropriately on inherited classes? Using generics? Extension methods?
Something else?
class baseClass
{
public static string ClassName()
{
return typeof(baseClass).FullName;
}
}
what I want is for:
class someClass : baseClass
{
....
}
and to be able to call someClass.ClassName() and have it return
"someClass"
obviously I can manually reimplement ClassName() in every inherited
subclass, but it strikes me that there should be -some- way of adding
a static method to a class that returns its own type name, without
having to do it manually for every inherited subclass.
-regards,
Dave
推荐答案
d-42写道:
d-42 wrote:
有没有办法让这个方法可以继承和hav它是否在继承的类上适当地表现了?b $ b?使用泛型?扩展方法?
别的什么?
class baseClass
{
public static string ClassName( )
{
返回typeof(baseClass).FullName;
}
}
我想要的是:
class someClass:baseClass
{
...
}
并且能够调用someClass.ClassName()并让它返回
" someClass"
显然我可以在每个继承的
子类中手动重新实现ClassName(),但是我觉得应该有一些添加
$ b $的方法ba静态方法,返回一个返回自己的类型名称的类,而不必为每个继承的子类手动执行
。
Is there any way to make this method inheritable and have it behave
appropriately on inherited classes? Using generics? Extension methods?
Something else?
class baseClass
{
public static string ClassName()
{
return typeof(baseClass).FullName;
}
}
what I want is for:
class someClass : baseClass
{
...
}
and to be able to call someClass.ClassName() and have it return
"someClass"
obviously I can manually reimplement ClassName() in every inherited
subclass, but it strikes me that there should be -some- way of adding
a static method to a class that returns its own type name, without
having to do it manually for every inherited subclass.
为什么:
someClass.ClassName()
优于:
typeof(someClass).FullName
?
Arne
Why is:
someClass.ClassName()
better than:
typeof(someClass).FullName
?
Arne
d-42< db ******** @ gmail.comwrote:
d-42 <db********@gmail.comwrote:
有没有办法使这个方法可以继承并让它在继承的类上适当地表现为
?使用泛型?扩展方法?
别的什么?
Is there any way to make this method inheritable and have it behave
appropriately on inherited classes? Using generics? Extension methods?
Something else?
否 - 静态部分实际上意味着在编译时解决。有没有像虚拟每种类型而不是每个实例这样的东西。成员
$ C $ b在C#中(虽然其他系统上存在这样的东西)。
No - the "static" part really means "resolved at compile-time". There''s
no such thing as a virtual "per type rather than per instance" member
in C# (although such things exist on other systems).
class baseClass
{
公共静态字符串ClassName()
{
返回typeof(baseClass).FullName;
}
}
我想要的是:
class someClass:baseClass
{
...
}
并且能够调用someClass.ClassName()并让它返回
someClass
很明显我可以手动重新实现每个继承的
子类中的ClassName(),但它让我觉得应该有-some - 向一个返回其自己的类型名称的类添加
a静态方法的方法,而不必为每个继承的子类手动执行
。
class baseClass
{
public static string ClassName()
{
return typeof(baseClass).FullName;
}
}
what I want is for:
class someClass : baseClass
{
...
}
and to be able to call someClass.ClassName() and have it return
"someClass"
obviously I can manually reimplement ClassName() in every inherited
subclass, but it strikes me that there should be -some- way of adding
a static method to a class that returns its own type name, without
having to do it manually for every inherited subclass.
当你编写someClass.ClassName()时,编译器会用生成的IL中的
baseClass.ClassName()替换它。
-
Jon Skeet - < sk *** @ pobox.com>
网站: http://www.pobox.com/~skeet
博客: http://www.msmvps.com/jon.skeet
C#深度: http://csharpindepth.com
When you write someClass.ClassName(), the compiler will replace it with
baseClass.ClassName() in the generated IL.
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
5月26日下午3:25,Arne Vajh?j< a ... @ vajhoej.dkwrote:
On May 26, 3:25 pm, Arne Vajh?j <a...@vajhoej.dkwrote:
d-42写道:
d-42 wrote:
有没有办法让这个方法可以继承并让它在继承的类上适当地运行?b $ b?使用泛型?扩展方法?
别的什么?
Is there any way to make this method inheritable and have it behave
appropriately on inherited classes? Using generics? Extension methods?
Something else?
class baseClass
{
公共静态字符串ClassName()
{
返回typeof(baseClass).FullName;
}
}
class baseClass
{
public static string ClassName()
{
return typeof(baseClass).FullName;
}
}
我想要的是:
what I want is for:
class someClass:baseClass
{
...
}
class someClass : baseClass
{
...
}
并且能够调用someClass.ClassName()并拥有它返回
someClass
and to be able to call someClass.ClassName() and have it return
"someClass"
显然我可以在每个继承的
子类中手动重新实现ClassName(),但它让我感到应该 - 某种方式将一个静态方法添加到一个返回其自己的类型名称的类中,而不必为每个继承的子类手动执行它。
obviously I can manually reimplement ClassName() in every inherited
subclass, but it strikes me that there should be -some- way of adding
a static method to a class that returns its own type name, without
having to do it manually for every inherited subclass.
为什么:
someClass.ClassName()
比:
typeof(someClass).FullName
?
Why is:
someClass.ClassName()
better than:
typeof(someClass).FullName
?
封装
优雅
我希望此类层次结构提供其类名作为部分它的
静态''API'',因为它的一些方法将这些类名称
作为输入。
- 欢呼,
Dave
encapsulation
elegance
I want this class hierarchy to provide its class name as part of its
static ''API'' because a number of its methods take those class names
as inputs.
-cheers,
Dave
这篇关于可继承的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!