访问被“新”隐藏的成员 [英] Accessing members which have been hidden by "new"

查看:55
本文介绍了访问被“新”隐藏的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


这个例子:


公共类BaseC

{

public int x;

public void Invoke(){}

}

公共类DerivedC:BaseC

{

new public void Invoke(){}

}


来自这里: http://msdn2.microsoft.com/en-us/library/435f1dw2.aspx

我可以使用BaseC.Invoke()访问原始(隐藏)方法Invoke()。


现在看看:


公共类BaseC

{

public int x;

public void Invoke(){}

}


公共类DerivedC:BaseC

{

public int y;

}


公共类DerivedDerivedC:DerivedC

{

public int z;

new public void Invoke(){}

}


如何访问原始文件(h idden)方法?


BaseC.Invoke()不起作用?为什么?


问候,罗伯特

Hello,

This example:

public class BaseC
{
public int x;
public void Invoke() {}
}
public class DerivedC : BaseC
{
new public void Invoke() {}
}

is from here: http://msdn2.microsoft.com/en-us/library/435f1dw2.aspx
I can access the original (hidden) method Invoke() by using BaseC.Invoke().

Now look at this:

public class BaseC
{
public int x;
public void Invoke() {}
}

public class DerivedC : BaseC
{
public int y;
}

public class DerivedDerivedC : DerivedC
{
public int z;
new public void Invoke() {}
}

How do I access the original (hidden) method ?

BaseC.Invoke() doesn''t work? Why?

regards, Robert

推荐答案

2008年4月21日星期一15: 34:04 -0700,Robert Fuchs< ni*@nix.atwrote:
On Mon, 21 Apr 2008 15:34:04 -0700, Robert Fuchs <ni*@nix.atwrote:

公共类BaseC {

public int x;

public void Invoke(){}

}

公共类DerivedC:BaseC

{

new public void Invoke(){}

}


来自这里: http://msdn2.microsoft.com/en-us/library/435f1dw2.aspx

我可以使用

BaseC.Invoke()访问原始(隐藏)方法Invoke()。
public class BaseC {
public int x;
public void Invoke() {}
}
public class DerivedC : BaseC
{
new public void Invoke() {}
}

is from here: http://msdn2.microsoft.com/en-us/library/435f1dw2.aspx
I can access the original (hidden) method Invoke() by using
BaseC.Invoke().



你的意思是base.Invoke(),来自

例子的实例方法? " BaseC.Invoke()"将暗示静态方法,并且Invoke()表示静态方法。是

在这种情况下不是静态方法。

Do you mean "base.Invoke()", as from within an instance method for
example? "BaseC.Invoke()" would imply a static method, and "Invoke()" is
not in this case a static method.


现在看看这个:


公共类BaseC {

public int x;

public void Invoke(){}

}


公共类DerivedC:BaseC

{

public int y;

}


public class DerivedDerivedC:DerivedC

{

public int z; new public void Invoke(){}

}


如何访问原始(隐藏)方法?


BaseC.Invoke()不起作用?为什么?
Now look at this:

public class BaseC {
public int x;
public void Invoke() {}
}

public class DerivedC : BaseC
{
public int y;
}

public class DerivedDerivedC : DerivedC
{
public int z; new public void Invoke() {}
}

How do I access the original (hidden) method ?

BaseC.Invoke() doesn''t work? Why?



再次,你的意思是base.Invoke()?


如果是这样的话,你可以'因为直接基类没有这样的

方法。但是,你可以将你的实例转换为BaseC并以这种方式访问​​

方法。


也就是说,隐藏一个几乎总是错误的无论如何,这是一个类成员。

你很容易让代码无法正常运行或按照预期运行,因为基类没有必要有什么方法可以知道

关于或使用隐藏的方法。


Pete

Again, do you mean "base.Invoke()"?

If so, then you can''t because the immediate base class has no such
method. However, you can cast your instance to BaseC and access the
method that way.

That said, it''s almost always a mistake to hide a class member anyway.
It''s very easy for code to fail to operate correctly or as expected when
you do so, because the base classes won''t necessarily have any way to know
about or use the hidden method.

Pete


如果有的话,那么你不能因为直接基类没有这样的
If so, then you can''t because the immediate base class has no such

方法。但是,您可以将实例强制转换为BaseC并以此方式访问

方法。
method. However, you can cast your instance to BaseC and access the
method that way.



我也考虑过铸造可以解决问题:

((BaseClass)this).MyMethod();

但是我有另一个问题:MyMethod()受到保护,我不想再改变它。


所以我得到了错误消息:

无法通过类型为''type1''的限定符访问受保护成员''member'';

限定符必须是''type2类型''(或从中衍生出来)"


有什么想法吗?


问候,罗伯特

I also thought about casting would do the trick:
((BaseClass)this).MyMethod();
But I have another problem: MyMethod() is protected, and I don''t want to
change that.

So I get the error message:
"Cannot access protected member ''member'' via a qualifier of type ''type1'';
the qualifier must be of type ''type2'' (or derived from it)"

Any idea?

regards, Robert


Peter Duniho写道:
Peter Duniho wrote:

2008年4月21日星期一15:34:04 -0700,Robert Fuchs< ni * @ nix.atwrote:
On Mon, 21 Apr 2008 15:34:04 -0700, Robert Fuchs <ni*@nix.atwrote:

>现在看看:

公共类BaseC {
public int x;
public void Invoke(){}
}
公共类DerivedC:BaseC
{
public int y;
}

public class DerivedDerived C:DerivedC
{/ / public public z; new public void Invoke(){}
}
如何访问原始(隐藏)方法?

BaseC.Invoke()不起作用?为什么?
>Now look at this:

public class BaseC {
public int x;
public void Invoke() {}
}

public class DerivedC : BaseC
{
public int y;
}

public class DerivedDerivedC : DerivedC
{
public int z; new public void Invoke() {}
}

How do I access the original (hidden) method ?

BaseC.Invoke() doesn''t work? Why?



再次,你的意思是base.Invoke()?


如果是这样的话,你可以'因为直接基类没有这样的

方法。


Again, do you mean "base.Invoke()"?

If so, then you can''t because the immediate base class has no such
method.



是的,从BaseC正确继承,base.Invoke()只需要工作。

工作。
< br $> b $ b -

J.
http ://symbolsprose.blogspot.com


这篇关于访问被“新”隐藏的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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