在c#中按名称覆盖? [英] override by name in c#?

查看:77
本文介绍了在c#中按名称覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c#是否支持覆盖名称而不是签名

例如我正在构建一个自定义集合类,那个ca />
只占用某种类型的对象

为此,我在类视图中添加了一个覆盖添加

并将其更改为

public int Add(object value

t

public int添加(MyItemClass值


这似乎有效,但它会生成一个警告''需要新的说明符

,因为这个方法隐藏了...... [基类的]。

但即使我认为这不会超越基础分支

因为它不是同一个签名,它确实如此,除了警告之外,我想要发生的是
。但是客户端仍然可以从bas中调用Add方法

类,或者它被完全覆盖只是被称为Add(即不需要sam

签名作为基类的方法来覆盖它)

Does c# support overriding by name and not signature
For instance I am building a custom collection class, that ca
only take a certain type of object
For this, I have added an override for add from the class view
and changed it fro
public int Add(object value
t
public int Add(MyItemClass value

This seems to work, but it generates a warning ''the new specifier is needed
because this method hides ...[that of the base class].
But even though I didn''t think this would work to override the base clas
because it''s not the same signature, it does, and apart from the warning, thi
is what I want to happen. But can a client still call the Add method from the bas
class, or is it totally overridden just by being called Add (i.e. doesn''t need the sam
signature as the base class''s method to override it)

推荐答案

如果你的集合类是从CollectionBase派生的,那么就不会发生
,因为没有Add方法可以覆盖。


创建自己的stronlgy类型集合的推荐方法是从CollectionBase派生



-

Bob Powell [MVP]

Visual C#,System.Drawing


图像过渡效果,自动持久配置和

设计时间鼠标操作全部在4月份发布的Well Formed
http ://www.bobpowell.net/wellformed.htm


用GDI + FAQ回答那些GDI +问题
http://www.bobpowell.net/gdiplus_faq.htm


GDI +常见问题RSS提要: http://www.bobpowell.net/faqfeed.xml

Windows窗体提示和技巧RSS: http://www.bobpowell.net/tipstricks.xml

鲍勃的博客:< a rel =nofollowhref =http://royo.is-a-geek.com/siteFeeder/GetFeed.aspx?FeedId=41target =_ blank> http://royo.is-a-geek .com / siteFeeder ... aspx?FeedId = 41

" songie D" <一个******* @ discussions.microsoft.com>在消息中写道

新闻:C9 ********************************** @ microsof t.com ...
If your collection class was derived from CollectionBase this wouldn''t
happen because there is no Add method to override.

The reccommended way of creating your own stronlgy typed collection is to
derive from CollectionBase.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Image transition effects, automatic persistent configuration and
design time mouse operations all in April''s issue of Well Formed
http://www.bobpowell.net/wellformed.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob''s Blog: http://royo.is-a-geek.com/siteFeeder...aspx?FeedId=41

"songie D" <an*******@discussions.microsoft.com> wrote in message
news:C9**********************************@microsof t.com...
c#是否支持覆盖名称而不是签名?
例如我正在构建一个自定义集合类,它只能采用某种类型的对象。
为此,我在类视图中添加了一个覆盖添加,
并将其从
public int添加(对象值)
到公众int Add(MyItemClass value)

这似乎有效,但它会生成一个警告''新的说明符是
,因为这个方法隐藏了...... [基类的] 。''
但即使我不认为这会覆盖基类
因为它不是相同的签名,它确实如此,除了警告,
这就是我想要发生的事情。但是客户端仍然可以从
基类中调用Add方法,或者只是通过调用Add来完全覆盖它(即不需要
需要与基类的方法相同的签名)覆盖它)
Does c# support overriding by name and not signature?
For instance I am building a custom collection class, that can
only take a certain type of object.
For this, I have added an override for add from the class view,
and changed it from
public int Add(object value)
to
public int Add(MyItemClass value)

This seems to work, but it generates a warning ''the new specifier is needed, because this method hides ...[that of the base class].''
But even though I didn''t think this would work to override the base class
because it''s not the same signature, it does, and apart from the warning, this is what I want to happen. But can a client still call the Add method from the base class, or is it totally overridden just by being called Add (i.e. doesn''t need the same signature as the base class''s method to override it)



songie D< an ******* @ discussion.microsoft.com>写道:
songie D <an*******@discussions.microsoft.com> wrote:
c#是否支持覆盖名称而不是签名?
例如我正在构建一个自定义集合类,它只能采用某种类型的对象。 />为此,我在类视图中添加了一个覆盖添加,
并将其从
public int添加(对象值)
到public int Add(MyItemClass)价值)


这不是覆盖 - 它没有覆盖修饰符。

这似乎有效,但它会产生警告''新的说明符是必需的,
因为这个方法隐藏了...... [基类的]。''


如果你改变了这看起来很奇怪签名 - 它不应该。


你能发一个简短但完整的程序来演示

问题吗?


请参阅 http://www.pobox.com/ ~sipet / csharp / complete.html for详细信息

我的意思是什么。

但即使我不认为这会覆盖基类
因为它是'不一样的签名,确实如此,除了警告之外,这是我想要发生的事情。但是客户端是否仍然可以从基类中调用Add方法,或者只是通过调用Add来完全覆盖它(即不需要与基类的方法相同的
签名)覆盖它)
Does c# support overriding by name and not signature?
For instance I am building a custom collection class, that can
only take a certain type of object.
For this, I have added an override for add from the class view,
and changed it from
public int Add(object value)
to
public int Add(MyItemClass value)
That''s not an override - it doesn''t have the override modifier.
This seems to work, but it generates a warning ''the new specifier is needed,
because this method hides ...[that of the base class].''
That seems odd if you''ve changed the signature - it shouldn''t.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
But even though I didn''t think this would work to override the base class
because it''s not the same signature, it does, and apart from the warning, this
is what I want to happen. But can a client still call the Add method from the base
class, or is it totally overridden just by being called Add (i.e. doesn''t need the same
signature as the base class''s method to override it)




它根本没有被覆盖 - 它被隐藏了。尝试用它来调用它:


BaseClass b = new DerivedClass();

b.Add(...);


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件



It isn''t overridden at all - it''s hidden. Try calling it with:

BaseClass b = new DerivedClass();
b.Add(...);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


尝试这首歌,它按照你的预期工作。


使用系统;


名称空间演示

{

///< summary>

///类的摘要说明。

///< / summary>

公共类Class1

{


public void Show(bool show)

{

if(show)

{

System.Windows.Forms.MessageBox.Show(&Show)来自Class1");

}

}

}


公共类Class2:Class1

{

public void Show(int show)

{

if(show> 0)

{

System.Windows.Forms.MessageBox.Show(" Show from Class2");

}

}

public static void Main()

{

Class1 c = new Class1();

c。展会(真); //从1级表示

Class2 d = new Class2();

d.Show(true); //从第1课开始(因为会员已被继承)

d.Show(1); //从第2课开始(因为会员有不同的签名

继承会员并创建超载会员


}

} < br $>
}


-


Br,

Mark Broadbent

mcdba,mcse + i

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

" songie D" an *******@discussions.microsoft.com>在留言中写道

新闻:C9 ********************** ************ @ microsof t.com ...
Try this songie, it works as you''d expect.

using System;

namespace Demo
{
/// <summary>
/// Summary description for Classes.
/// </summary>
public class Class1
{

public void Show(bool show)
{
if (show)
{
System.Windows.Forms.MessageBox.Show("Show from Class1");
}
}
}

public class Class2: Class1
{
public void Show(int show)
{
if (show>0)
{
System.Windows.Forms.MessageBox.Show("Show from Class2");
}
}
public static void Main()
{
Class1 c = new Class1();
c.Show(true); //show from class 1
Class2 d = new Class2();
d.Show(true); //show from class 1 (because member has been inherited)
d.Show(1); //show from class 2 (because member has different signature of
inherited member and creates overloaded member

}
}
}


--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"songie D" <an*******@discussions.microsoft.com> wrote in message
news:C9**********************************@microsof t.com...
c#是否支持覆盖名称而不是签名?
例如我正在构建自定义集合类,它只能占用某种类型的对象。
为此,我在类视图中添加了一个覆盖添加,
并将其从
public int添加(对象值)
公共int添加(MyItemClass值)

这似乎有效,但它会生成一个警告''新的说明符是
所需,因为这种方法隐藏了...... [基类的方法]。''
但即使我不认为这会覆盖基类
因为它不是相同的签名,它确实如此,除了警告,
这就是我想要的即将发生。但是客户端仍然可以从
基类中调用Add方法,或者只是通过调用Add来完全覆盖它(即不需要
需要与基类的方法相同的签名)覆盖它)
Does c# support overriding by name and not signature?
For instance I am building a custom collection class, that can
only take a certain type of object.
For this, I have added an override for add from the class view,
and changed it from
public int Add(object value)
to
public int Add(MyItemClass value)

This seems to work, but it generates a warning ''the new specifier is needed, because this method hides ...[that of the base class].''
But even though I didn''t think this would work to override the base class
because it''s not the same signature, it does, and apart from the warning, this is what I want to happen. But can a client still call the Add method from the base class, or is it totally overridden just by being called Add (i.e. doesn''t need the same signature as the base class''s method to override it)



这篇关于在c#中按名称覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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