可以覆盖基类中的静态成员吗​​? [英] Possible to override static member in base class?

查看:42
本文介绍了可以覆盖基类中的静态成员吗​​?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来语言试图阻止我做这种事情。然而,以下编译,我想知道为什么它不会以它应该的方式工作:


公共类ComponentA

{

静态字符串s_name ="我是根类。" ;;

公共字符串名称{get {return s_name;}}

}


公共类ComponentB:ComponentA

{

new static string s_name ="我是派生类。;

}


static void Main()

{

ComponentB b = new ComponentB();

Console.WriteLine(" ComponentB说:" + b.Name);

}


我想要这样说我是派生类。但它仍然说我是根>

类。


我的目标是派出一大堆我可以得到的课程

描述两个

- 通过反映类类型(因此需要静态成员)

- 通过使用a的类的实例(在根类中定义

的虚拟成员。


这可能吗?

It looks like the language is trying to prevent me from doing this sort of
thing. Nevertheless, the following compiles, and I''d like to know why it
doesn''t work the way it should:

public class ComponentA
{
static string s_name = "I am the root class.";
public string Name { get {return s_name;} }
}

public class ComponentB : ComponentA
{
new static string s_name = "I am a derived class.";
}

static void Main()
{
ComponentB b = new ComponentB();
Console.WriteLine("ComponentB says: " + b.Name);
}

I want this to say "I am a derived class." but it still says "I am the root
class."

My objective is to derive a whole bunch of classes on which I can get a
description both
- Through reflection of the class Type (thus requiring the static member)
- Through an instance of the class using a (virtual) member that is defined
in the root class.

Is this possible?

推荐答案

>我的目标是派生一大堆类,我可以在其上得到
>My objective is to derive a whole bunch of classes on which I can get a
描述
- 通过反映类类型(因此需要静态)成员)
- 通过使用在根类中定义的(虚拟)成员的类的实例。

这可能吗?
description both
- Through reflection of the class Type (thus requiring the static member)
- Through an instance of the class using a (virtual) member that is defined
in the root class.

Is this possible?



为什么你不要将Name属性设为虚拟并在

派生类中覆盖它。

Mattias
< br $> b $ b -

Matti作为Sj?gren [C#MVP] mattias @ mvps.org
http:// www.msjogren.net/dotnet/ | http://www.dotnetinterop.com

请回复到新闻组。



Why don''t you make the Name property virtual and override it in the
derived classes.
Mattias

--
Mattias Sj?gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


你好dbooksta,


感谢你的帖子!


是的,C#不支持静态虚拟成员。这是因为:要实现虚拟成员,编译器必须将此成员作为

条目放在v-table中,该表附加到类实例。静态成员

不会附加到任何类实例,而是附加到类本身,因此

静态成员不能在子类中虚拟和覆盖。


你最好采用Mattias的建议来标记姓名。实例属性为

虚拟属性,并在继承的类中覆盖它。

下面的代码片段演示了:


公共类ComponentA

{

静态字符串s_name ="我是根类。" ;;

公共虚拟字符串名称{get {return s_name;}}

}


公共类ComponentB:ComponentA

{

new static string s_name ="我是派生类。" ;;

public覆盖字符串名称{get {return s_name;}}

}


private void button1_Click(object sender,System.EventArgs e)

{

ComponentB b = new ComponentB();

Console.WriteLine(" ComponentB说:" + b.Name);

}

希望这会有所帮助。


祝你好运,

Jeffrey Tan

Microsoft Online社区支持

======================================== ==========

在回复帖子时,请回复群组通过你的新闻阅读器

其他人可以从你的问题中学习并从中受益。

==================== ==============================

此帖子按原样提供。没有保证,也没有授予任何权利。

Hi dbooksta,

Thanks for your post!

Yes, C# does not support static virtual member. This is because: to
implement a virtual member, the complier has to place this member as an
entry in the v-table, which attaches to a class instance. Static member
does not attach to any class instance but attaches to the class itself, so
static member can not be virtual and overrided in child class.

You''d better take Mattias'' suggestion to mark "Name" instance property as a
virtual one and override it in inherited class. The code snippet below
demonstrates this:

public class ComponentA
{
static string s_name = "I am the root class.";
public virtual string Name { get {return s_name;} }
}

public class ComponentB : ComponentA
{
new static string s_name = "I am a derived class.";
public override string Name { get {return s_name;} }
}

private void button1_Click(object sender, System.EventArgs e)
{
ComponentB b = new ComponentB();
Console.WriteLine("ComponentB says: " + b.Name);
}
Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


好的,这将有效。但只是为了确认:没有办法定义一个

接口,抽象或基类,以便它需要一个特定的

静态成员(比如s_name, )在派生类中定义或重新定义?


如果是这样,它似乎是一种语言缺点,不是吗?

"" Jeffrey Tan [MSFT]""写道:
OK, this will work. But just to confirm: Is there no way to define an
interface, abstract, or base class such that it REQUIRES that a particular
static member (say "s_name") be defined or redefined in a derived class?

If so, it seems like a language shortcoming, doesn''t it?
""Jeffrey Tan[MSFT]"" wrote:
您好dbooksta,

感谢您的帖子!

是的,C#不支持静态虚拟成员。这是因为:要实现虚拟成员,编译器必须将此成员作为附加到v-table中的条目,该表附加到类实例。静态成员
不会附加到任何类实例,而是附加到类本身,因此
静态成员不能在子类中虚拟和覆盖。

你最好采取Mattias的建议来标记姓名实例属性为虚拟属性,并在继承的类中覆盖它。下面的代码片段说明了这一点:

公共类ComponentA
{
静态字符串s_name ="我是根类。" ;;
public virtual string Name {get {return s_name;}}
}
公共类ComponentB:ComponentA
{
新的静态字符串s_name ="我是派生的class。" ;;
公共覆盖字符串名称{get {return s_name;}}
}
私有void button1_Click(对象发送者,System.EventArgs e)
{
ComponentB b = new ComponentB();
Console.WriteLine(" ComponentB说:" + b.Name);
}
希望这有帮助。

致以最诚挚的问候,
Jeffrey Tan
微软在线社区支持
======================== ==========================
在回复帖子时,请回复群组通过您的新闻阅读器,以便其他人可以从您的问题中学习并从中受益。
============================ ======================
此帖子是原样提供的。没有保证,也没有授予任何权利。
Hi dbooksta,

Thanks for your post!

Yes, C# does not support static virtual member. This is because: to
implement a virtual member, the complier has to place this member as an
entry in the v-table, which attaches to a class instance. Static member
does not attach to any class instance but attaches to the class itself, so
static member can not be virtual and overrided in child class.

You''d better take Mattias'' suggestion to mark "Name" instance property as a
virtual one and override it in inherited class. The code snippet below
demonstrates this:

public class ComponentA
{
static string s_name = "I am the root class.";
public virtual string Name { get {return s_name;} }
}

public class ComponentB : ComponentA
{
new static string s_name = "I am a derived class.";
public override string Name { get {return s_name;} }
}

private void button1_Click(object sender, System.EventArgs e)
{
ComponentB b = new ComponentB();
Console.WriteLine("ComponentB says: " + b.Name);
}
Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.



这篇关于可以覆盖基类中的静态成员吗​​?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆