使用“新”的班级成员中的关键字。 [英] Use of the "new" keyword in class members.

查看:103
本文介绍了使用“新”的班级成员中的关键字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以向我解释一下新的使用情况。关键字(Shadows
VB中的
)。


我想我明白它是如何工作的但是我无法弄清楚你为什么这么做b $ b会用它。为什么要在派生的

类中声明一个变量,其名称与基类中的名称相同但是具有不同的含义

意思是什么?

任何人都可以想到这个场景吗?


***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!

Can anyone explain to me a meningfull use of the "new" keyword (Shadows
in VB).

I think i understand how it works fully but i cant figure out why you
would use it. Why would you want to declare a variable in a derived
class with the same name as in the base class but with a diffrent
meaning?

Anyone can think of a scenario for this?

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!

推荐答案

Ola Johansson< ol*@whoa.nu>写道:
Ola Johansson <ol*@whoa.nu> wrote:
任何人都可以向我解释一下新的使用情况。关键字(VB中的阴影
)。

我想我理解它是如何工作的但我无法弄清楚为什么你会使用它。为什么要在派生的
类中声明一个变量,其名称与基类中的名称相同但具有不同的含义?

任何人都可以想到一个场景这个?
Can anyone explain to me a meningfull use of the "new" keyword (Shadows
in VB).

I think i understand how it works fully but i cant figure out why you
would use it. Why would you want to declare a variable in a derived
class with the same name as in the base class but with a diffrent
meaning?

Anyone can think of a scenario for this?




是的。假设你派生自另一个类,在写作时没有一个名为Foo的成员。你包括一个,它就会成为你公共界面的一部分。现在,稍后基类获得

a成员称为Foo - 您有三种选择:


1)使用new隐藏基本成员

2)重命名你的会员

3)只使用班级的旧版本


2和3可能不是可行的选择,离开第一个。发现自己总是一个不幸的情况,但这就是为什么

就在那里。


-

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

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



Yes. Suppose you derive from another class, which at the time of
writing doesn''t have a member called Foo. You include one, and it
becomes part of your public interface. Now, later the base class gains
a member called Foo - you have three choices:

1) Hide the base member using "new"
2) Rename your member
3) Only use the old version of the class

2 and 3 may well not be viable options, leaving the first one. It''s
always an unfortunate situation to find yourself in, but that''s why
it''s there.

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


假设我设计了自己的消息框表单,并实现了我自己的Show()方法。我

不要让他们使用底座所以我用new来说我隐藏底座和

使用我自己的。

" Ola约翰森" < ol*@whoa.nu>在留言中写道

news:uQ ************** @ TK2MSFTNGP12.phx.gbl ...
Say I design my own messagebox form, and implement my own Show() method. I
dont want them to use the base so I use new to say im hiding the base and
using my own.
"Ola Johansson" <ol*@whoa.nu> wrote in message
news:uQ**************@TK2MSFTNGP12.phx.gbl...
任何人都可以向我解释一个meningfull使用新的关键字(VB中的阴影
)。

我想我理解它是如何工作的但我无法弄清楚为什么你会使用它。为什么要在派生的
类中声明一个变量,其名称与基类中的名称相同但具有不同的含义?

任何人都可以想到一个场景这个?

***通过开发人员指南 http://www.developersdex发送。 com ***
不要只是参加USENET ......获得奖励!
Can anyone explain to me a meningfull use of the "new" keyword (Shadows
in VB).

I think i understand how it works fully but i cant figure out why you
would use it. Why would you want to declare a variable in a derived
class with the same name as in the base class but with a diffrent
meaning?

Anyone can think of a scenario for this?

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



< di ********@discussion.microsoft.com>写道:
<di********@discussion.microsoft.com> wrote:
假设我设计了自己的消息框表单,并实现了我自己的Show()方法。我不希望他们使用底座所以我使用new来说我隐藏底座和
使用我自己的。
Say I design my own messagebox form, and implement my own Show() method. I
dont want them to use the base so I use new to say im hiding the base and
using my own.




我建议*不*那样做。 IMO,有意识地隐藏会员,这不是一个坏主意。我只想给会员一个

不同的名字。否则用户可能会感到困惑,并相信他们会打电话给原来的。


-

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

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



I would suggest *not* doing that. Hiding members deliberately when
there''s no need to is a bad idea, IMO. I would just give the member a
different name. Otherwise users may well get confused and believe
they''re calling the original one.

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


这篇关于使用“新”的班级成员中的关键字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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