"隐藏"一个继承的财产? [英] "Hiding" an inherited property?

查看:97
本文介绍了"隐藏"一个继承的财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让

程序员看不到继承类的属性?我知道使用关键字new允许你创建

另一个属性代替现有属性,但是如何隐藏

属性并且根本不可用?

谢谢,

雅各布

Is there a way to make a property of an inherited class invisible to the
programmer? I know that using the keyword "new" allows you to create
another property in the place of the existing one, but how can I make the
property hidden and not available at all?
Thanks,
Jacob

推荐答案

雅各布,


我相信你试图通过这样做来重写继承的基本原理。


官方的方法是在你的
handler。


想象一下以下场景:


class MyBase

{

public virtual void DoSomething()

{

}

}


class MySubclass

{

public overrides void DoSomething()

{

抛出新的NotImplementException(); < br $>
}

}


即使你可以让MySubclass :: DoSomething看不见它仍会
$ b $通过它的超级班级可以看到


例如


MyBase mb = new MySubclass();

MyBase.DoSomething();


这就是为什么你需要引发NotImplementException以防止调用

,无论在类的哪个层次调用该函数。


希望清除它了,


欢呼,

g


PS我想做的同样有时但你别无选择,只需要在食物链中进一步分类,或者可能使用封装

而不是继承来提供你需要的功能。


" Jacob" < JA ********** @ REMOVETHIShotmail.com>在消息中写道

news:7_Uec.6111
Jacob,

I believe you''re trying to re-write the fundamentals of inheritance by doing
this.

The official way is to raise an NotImplemented exception within your
handler.

Imagine the following scenario:

class MyBase
{
public virtual void DoSomething()
{
}
}

class MySubclass
{
public overrides void DoSomething()
{
throw new NotImplementException();
}
}

even if you could make the MySubclass::DoSomething invisible it would still
be visible through it''s super class

e.g.

MyBase mb = new MySubclass();
MyBase.DoSomething();

This is why you need to raise the NotImplementException to prevent calling
this no matter where in the class hierachy that function is called from.

hope that clears it up,

cheers,

g

PS I''ve wanted to do the same sometimes but you''ve no choice but to either
subclass a class further up in the food chain or possibly use encapsulation
rather than inheritance to provide the functionality you require.

"Jacob" <ja**********@REMOVETHIShotmail.com> wrote in message
news:7_Uec.6111


ab3.1674@fed1read02 ...
ab3.1674@fed1read02...
有没有办法制作一个
程序员看不见的继承类的属性?我知道使用关键字new允许你创建
另一个属性代替现有属性,但是如何隐藏
属性并且根本不可用?

谢谢,
Jacob
Is there a way to make a property of an inherited class invisible to the
programmer? I know that using the keyword "new" allows you to create
another property in the place of the existing one, but how can I make the
property hidden and not available at all?
Thanks,
Jacob



雅,我从来没有找到办法,我只是希望别人知道一个。

使用NotImplemented异常是一个很好的做法,感谢提示。

我可以发誓微软已经在他们的一些课程上完成了它。我现在不能想到一个特定的例子,但是我确定我已经在某个地方看到了它的价值。


谢谢,

雅各布

" Gary Hunt" <是******* @ codequest.co.uk>在消息中写道

news:ur ************* @ tk2msftngp13.phx.gbl ...
Ya, I''ve never found a way either, I was just hoping someone else knew one.
Using the NotImplemented exception is a good practice, thanks for the tip.
I could swear that Microsoft has done it on some of their classes though. I
can''t think of a particular example right now, but I''m certain that I''ve
seen it somewhere.

Thanks,
Jacob
"Gary Hunt" <be*******@codequest.co.uk> wrote in message
news:ur*************@tk2msftngp13.phx.gbl...
Jacob,

我相信你试图用
来重写这些继承的基本原理。

正式的方法是在你的内部引发一个NotImplemented异常
处理器。

想象一下以下场景:

类MyBase
{
public virtual void DoSomething()


}

类MySubclass
{
公开覆盖void DoSomething()
{
抛出新的NotImplementException();
}
}

即使你可以让MySubclass :: DoSomething看不见它也会通过它的超级类看到


例如

MyBase mb = new MySubclass();
MyBase.DoSomething();

这就是为什么你需要引发NotImplementException以防止调用
这个无论在课堂的哪个层面都有这个功能来自。

希望能搞清楚,欢呼,



PS我已经我们有时也想做同样的事情,但你别无选择,只能在食物链中进一步继承一个类,或者使用
封装而不是继承来提供你需要的功能。

雅各布 < JA ********** @ REMOVETHIShotmail.com>在消息中写道
新闻:7_Uec.6111
Jacob,

I believe you''re trying to re-write the fundamentals of inheritance by doing this.

The official way is to raise an NotImplemented exception within your
handler.

Imagine the following scenario:

class MyBase
{
public virtual void DoSomething()
{
}
}

class MySubclass
{
public overrides void DoSomething()
{
throw new NotImplementException();
}
}

even if you could make the MySubclass::DoSomething invisible it would still be visible through it''s super class

e.g.

MyBase mb = new MySubclass();
MyBase.DoSomething();

This is why you need to raise the NotImplementException to prevent calling
this no matter where in the class hierachy that function is called from.

hope that clears it up,

cheers,

g

PS I''ve wanted to do the same sometimes but you''ve no choice but to either
subclass a class further up in the food chain or possibly use encapsulation rather than inheritance to provide the functionality you require.

"Jacob" <ja**********@REMOVETHIShotmail.com> wrote in message
news:7_Uec.6111


这篇关于&QUOT;隐藏&QUOT;一个继承的财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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