继承 - 限制方法等 [英] Inheritance - restrict methods etc.

查看:54
本文介绍了继承 - 限制方法等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我希望能够创建一个继承Label的类。我想

为我的新班级定义了许多属性和方法。


问题:

我可以阻止一个这个新类的用户访问所有属性

和基础Label的方法。类。一些,如文字将需要

可用。


我将不胜感激任何建议。


艺术

推荐答案

" Art" < Ar*@discussions.microsoft.com> schrieb:
"Art" <Ar*@discussions.microsoft.com> schrieb:
我希望能够创建一个继承Label的类。我想为我的新课程定义一些属性和方法。

问题:
我可以阻止这个新类的用户访问所有的
属性
和基础标签的方法。类。一些,如文字将需要
I''d like to be able to create a class that inherits "Label". I want to
define a number of properties and methods for my new class.

The Question:
Can I prevent a user of this new class from accessing all of the
properties
and methods of the base "Label" class. Some, like "Text" will need to be
available.




我只是好奇你为什么要那样做。继承用于扩展现有类,并且隐藏基类的方法与

相冲突。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB < URL:http://classicvb.org/petition/>



I an just curious why you would want to do that. Inheritance is used to
extend an existing class, and hiding methods of a base class stands in
conflict with this principle.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


我同意整个''合同''的事情,但合同CAN有精美的印刷品:)


如果我想创造一种新的汽车,例如可以根据我的脑电波驱动自己

那么,给你的例如我仍然需要实施一个方向盘才能实现一个方向盘,因为我开始使用

a汽车,而且只有一辆。


也许我想从一辆汽车开始,并派出一个完全不同类型汽车的类别?
?这是否意味着我必须重新启动

重写代码,重新修改除了我想要的1%以外的代码并将其用作

a基类?


同样,虽然我同意最纯粹的形式,继承确实意味着某些事情,但我也认为它最终可能会受到影响。

特别是如果我打算密封我的班级。

" Carlos J. Quintero [.NET MVP]" < CA ***** @ NOSPAMsogecable.com>写在

消息新闻:%2 ****************** @ TK2MSFTNGP15.phx.gbl ...
I agree on the entire ''contract'' thing, but contracts CAN have fine print :)

If I want to create a new kind of car that for example can drive itself
based upon my brain waves then, given your example I should still have to
implement a steering wheel just because of the fact that I started out with
a car that had one.

Perhaps I want to start off with a Car and derive a class that is a
completely different type of car? Does this mean that I have to start out
rewriting code that reimpliments all but the 1% that I want and use that as
a base class?

Again, while I agree that in its purest form, Inheritance does imply certain
things, but I also think that it can end up getting in the way a bit.
Especially if I plan to seal my class.
"Carlos J. Quintero [.NET MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:%2******************@TK2MSFTNGP15.phx.gbl...
雷卡西克 < RC ****** @ nospam.enterprocity.com> escribióenel mensaje
新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
"Ray Cassick" <rc******@nospam.enterprocity.com> escribió en el mensaje
news:%2****************@TK2MSFTNGP10.phx.gbl...
我试过做这也是......人们总是似乎回来了那个标准''打破继承规则''仍然没有和我一起坐下来的事情。
如果我想继承一个特定的基类并密封我的类,所以它不能继承,那么我不应该有任何理由来实现和公开任何基类方法。
如果你使用继承,你正在签订合同,就像你说你的类实现了一些接口。如果你的班级是一辆车
I have tried to do this also.. People always seem to come back with that
standard ''breaks the rule of inheritance'' thing that still does not sit
right with me.
If I want to inherit from a specific base class and seal my class so it
cannot be inherited from then there should not be any reason I HAVE to
implement and expose any of the base classes methods.
If you use inheritance, you are signing a contract, like when you say that
your class implements some interface. If your class is a Car which



继承自车辆,你不能假装它没有车轮......所有功能
期待车辆(任何派生类)将假设它有轮子。
如果那不符合你的需要,那你就不能继承,你必须使用其他东西(比如用委托封装)

继承可能需要一些时间来理解,但是当你这样做时,它会有意义。

-

致以最诚挚的问候,

卡洛斯J. Quintero

MZ-Tools:Visual Studio .NET,VB6,VB5和VBA的生产力插件
您可以更快地编码,设计和记录。
免费资源对于加载项开发人员:
http://www.mztools.com



一个很好的例子就是一个类,它从另一个处理它的类中获取它的b / b
自己的绑定和不想使用斯坦dard绑定但你仍然有
来处理基类绑定方法等。


有一个属性或一些这样的隐藏基础会很好class

用户不想要的属性和方法。如果一个人不想这样做,那么就不要使用这个属性了。


" Herfried K. Wagner [MVP] "写道:
A good example is a class that inheirits from another class that handles it''s
own binding and doesn''t want to use the standard binding yet you still have
to handle the base class binding methods, etc.

It would be nice to have an attribute or some such for hiding base class
properties and methods that the user doesn''t want. If one doesn''t want to do
this, then just don''t use this attribute.

"Herfried K. Wagner [MVP]" wrote:
" Art" < Ar*@discussions.microsoft.com> schrieb:
"Art" <Ar*@discussions.microsoft.com> schrieb:
我希望能够创建一个继承Label的类。我想为我的新课程定义一些属性和方法。

问题:
我可以阻止这个新类的用户访问所有的
属性
和基础标签的方法。类。一些,如文字将需要
I''d like to be able to create a class that inherits "Label". I want to
define a number of properties and methods for my new class.

The Question:
Can I prevent a user of this new class from accessing all of the
properties
and methods of the base "Label" class. Some, like "Text" will need to be
available.



我只是好奇你为什么要那样做。继承用于扩展现有类,隐藏基类的方法与此原则相冲突。

-
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/>
VB< URL:http://classicvb.org/petition/>



I an just curious why you would want to do that. Inheritance is used to
extend an existing class, and hiding methods of a base class stands in
conflict with this principle.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>



这篇关于继承 - 限制方法等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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