因重载而感到困惑(或者我可以声称它是编译器错误......?) [英] Confused by overloading (or can I claim it's a compiler bug...?)

查看:89
本文介绍了因重载而感到困惑(或者我可以声称它是编译器错误......?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下行为与...一样?


公共类基地

{

public void添加(字节b)

{

}

}


公共类TestClass:Base

{

public void Add(int s)

{

byte x = 0;

添加(x);

}

}


然后拨打

新的TestClass ()。Add(10);


- 调用Add(int)覆盖(好)。然后反过来分类

___添加(int)___覆盖。


为什么这第二次调用没有达到基类的添加(字节) )??


[如果Add(byte)方法在

TestClass而不是Base中定义,或者你调用它,它就像我预期的那样工作base.Add(x)]

Why does the following behave as it does...?

public class Base
{
public void Add( byte b )
{
}
}

public class TestClass : Base
{
public void Add( int s )
{
byte x = 0;
Add( x );
}
}

then call with
new TestClass().Add( 10 );

-The Add(int) override gets called (good). Which in turn class the
___Add(int)___ override.

Why doesn''t this second call hit the base class''s Add( byte ) ??

[It works as I''d expected if the Add(byte) method is defined in
TestClass rather than Base, or you call base.Add(x) ]

推荐答案

3月26日上午11:05,AJ< n ... @ nowhere.comwrote:
On Mar 26, 11:05 am, AJ <n...@nowhere.comwrote:

为什么以下行为与...一样?


公共类基地

{

public void添加(字节b)

{

}

}

公共类TestClass:Base

{

public void Add(int s)

{

字节x = 0;

添加(x);

}

}


然后打电话给

新的TestClass()。添加(10);


- 调用Add(int)覆盖(好)。然后反过来分类

___添加(int)___覆盖。


为什么这第二次调用没有达到基类的添加(字节) )??


[如果Add(byte)方法在

TestClass而不是Base中定义,或者你调用它,它就像我预期的那样工作base.Add(x)]
Why does the following behave as it does...?

public class Base
{
public void Add( byte b )
{
}
}

public class TestClass : Base
{
public void Add( int s )
{
byte x = 0;
Add( x );
}
}

then call with
new TestClass().Add( 10 );

-The Add(int) override gets called (good). Which in turn class the
___Add(int)___ override.

Why doesn''t this second call hit the base class''s Add( byte ) ??

[It works as I''d expected if the Add(byte) method is defined in
TestClass rather than Base, or you call base.Add(x) ]



查看编译器错误;你应该得到一个警告

TestClass隐藏了基类的成员。您需要将

虚拟关键字添加到基类中的方法定义中,然后将overbides关键字添加到子类中的方法定义中。如果

你需要打电话给基地,你需要自己做。

Look at your compiler errors; you should be getting a warning that
TestClass is hiding a member of the base class. You need to add the
virtual keyword to the method definition in the base class, then add
the overrides keyword to the method definition in the subclass. If
you need to call the base, you need to do so yourself.


这是不正确的。这里没有隐藏。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Andy" < an *** @ med-associates.comwrote in message

news:11 ********************* @ y80g2000hsf.googlegro ups.com ...
That''s not correct. There is no hiding here.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andy" <an***@med-associates.comwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...

3月26日上午11点05分,AJ< n ... @ nowhere.comwrote:
On Mar 26, 11:05 am, AJ <n...@nowhere.comwrote:

>为什么以下行为与...一样?

公共类基地
{
public void Add(byte b)
{
}

公共类TestClass:Base
{
public void Add(int s)
{
byte x = 0;
添加(x);
}
}
然后用
新的TestClass()调用。添加(10 );

- 调用Add(int)覆盖(好)。反过来又上课了
___ Add(int)___覆盖。

为什么第二次调用没有命中基类'的Add(byte)??

[如果Add(byte)方法在
TestClass而不是Base中定义,或者你调用base.Add(x)]
>Why does the following behave as it does...?

public class Base
{
public void Add( byte b )
{
}
}

public class TestClass : Base
{
public void Add( int s )
{
byte x = 0;
Add( x );
}
}

then call with
new TestClass().Add( 10 );

-The Add(int) override gets called (good). Which in turn class the
___Add(int)___ override.

Why doesn''t this second call hit the base class''s Add( byte ) ??

[It works as I''d expected if the Add(byte) method is defined in
TestClass rather than Base, or you call base.Add(x) ]



查看编译器错误;你应该得到一个警告

TestClass隐藏了基类的成员。您需要将

虚拟关键字添加到基类中的方法定义中,然后将overbides关键字添加到子类中的方法定义中。如果你需要打电话给基地,你需要自己打电话。


Look at your compiler errors; you should be getting a warning that
TestClass is hiding a member of the base class. You need to add the
virtual keyword to the method definition in the base class, then add
the overrides keyword to the method definition in the subclass. If
you need to call the base, you need to do so yourself.



在文章< 11*********************@y80g2000hsf.googlegroups。 com>,
an***@med-associates.com 说...
In article <11*********************@y80g2000hsf.googlegroups. com>,
an***@med-associates.com says...

3月26日上午11:05,AJ< n ... @ nowhere.comwrote:
On Mar 26, 11:05 am, AJ <n...@nowhere.comwrote:

为什么以下表现如下......?


公共类基地

{

public void Add(byte b)

{

}

}


公共类TestClass:Base

{

public void Add(int s)

{

byte x = 0;

Add(x) ;

}

}


然后打电话给

新的TestClass()。添加(10 );


- 调用Add(int)覆盖(好)。然后反过来分类

___添加(int)___覆盖。


为什么这第二次调用没有达到基类的添加(字节) )??


[如果Add(byte)方法在

TestClass而不是Base中定义,或者你调用它,它就像我预期的那样工作base.Add(x)]
Why does the following behave as it does...?

public class Base
{
public void Add( byte b )
{
}
}

public class TestClass : Base
{
public void Add( int s )
{
byte x = 0;
Add( x );
}
}

then call with
new TestClass().Add( 10 );

-The Add(int) override gets called (good). Which in turn class the
___Add(int)___ override.

Why doesn''t this second call hit the base class''s Add( byte ) ??

[It works as I''d expected if the Add(byte) method is defined in
TestClass rather than Base, or you call base.Add(x) ]



查看编译器错误;你应该得到一个警告

TestClass隐藏了基类的成员。您需要将

虚拟关键字添加到基类中的方法定义中,然后将overbides关键字添加到子类中的方法定义中。如果

你需要打电话给基地,你需要自己做。


Look at your compiler errors; you should be getting a warning that
TestClass is hiding a member of the base class. You need to add the
virtual keyword to the method definition in the base class, then add
the overrides keyword to the method definition in the subclass. If
you need to call the base, you need to do so yourself.



我超载了,不是压倒......

I''m overloading, not overriding..


这篇关于因重载而感到困惑(或者我可以声称它是编译器错误......?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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