为什么要使用new修饰符? [英] Why use the new modifier?

查看:80
本文介绍了为什么要使用new修饰符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说当你想要派生的

类中的成员隐藏基类中同名的成员时,你应该使用new:


class A

{

public int total;

}


B级:A

{

公共新浮动总数;

}


但是省略它只会导致警告,没有任何错误,并且该类似乎以相同的方式工作

。它有什么实际意义吗?


谢谢,


-

Peter Aitken


在使用之前从我的电子邮件地址中删除垃圾。

Supposedly you are supposed to use new when you want a member in a derived
class to hide a member of the same name in the base class:

class A
{
public int total;
}

class B:A
{
public new float total;
}

But omitting it causes only a warning, no error, and the class seems to work
the same either way. Does it have any real purpose?

Thanks,

--
Peter Aitken

Remove the crap from my email address before using.

推荐答案

确定它有目的。它使警告沉默。


你的目标不仅仅是创建有效的程序,而且可以修改一个工作_AND_可以修改的程序(由其他人修改)工作,稍后当

要求改变时。


当某些维护程序员尝试重新编译你的代码时,看到

警告,他可能会说,愚蠢的程序员;他不需要总计B,

,因为A中已有一个。并删除它 - 程序将

失败。通过添加新和新你明确地告诉所有可能会看到这个

代码的人,是的,该死的,我想要一个总数在B中,'分开&不一致的

来自A" ;.


-

-

真相,

James Curran

[以前的VC ++ MVP]


主页: www.noveltheory.com 工作: www.njtheater.com

博客: www.honestillusion.com 日间工作: www.partsearch.com


" Peter Aitken" < PA ***** @ CRAPnc.rr.com>在消息中写道

新闻:#6 ************** @ TK2MSFTNGP09.phx.gbl ...
Sure it has a purpose. It silences the warning.

Your goal here is not merely to create programs that work, but one that
work _AND_ can be modified (by someone else) to work, later when the
requirements change.

When some maintance programmer tries recompiling your code, and sees the
warning, he''ll probably say, "Silly programmer; he doesn''t need total in B,
because there''s already one in A." and remove it --- and the program will
fail. By adding the "new" you are explicitly saying to all who may see this
code later, "Yes, dammit, I want a "total" in B that''s separate & distinct
from the total in A".

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Peter Aitken" <pa*****@CRAPnc.rr.com> wrote in message
news:#6**************@TK2MSFTNGP09.phx.gbl...
据说你应该是当你想要一个派生的类中的成员隐藏基类中同名的成员时使用new:

class A
{
public int总计;
}

B班:A
公共新的总数;
}

但是省略它会导致只有一个警告,没有错误,并且类似乎
的工作方式相同。它有什么真正的目的吗?

谢谢,

-
Peter Aitken

在使用之前从我的电子邮件地址中删除废话。
Supposedly you are supposed to use new when you want a member in a derived
class to hide a member of the same name in the base class:

class A
{
public int total;
}

class B:A
{
public new float total;
}

But omitting it causes only a warning, no error, and the class seems to work the same either way. Does it have any real purpose?

Thanks,

--
Peter Aitken

Remove the crap from my email address before using.



如果你发现自己在想我需要在代码中使用new修饰符你正在写一个大的红色闪烁灯墙应该开始闪烁文字


警告!警告!设计FLAW!设计FLAW!


新修饰符用于处理基类(您无法控制)引入名称与派生类已经拥有的成员冲突的成员的情况。在这种情况下,编译器会警告您存在理论上的歧义(尽管发生的事情已明确定义)。它要求您指定此碰撞是否打算覆盖或者您的方法是否与基类版本完全分离(后者是在运行时如果您什么也不做的话)。


C#编译器要求你*明确*关于你的意图,这是C#中的一个共同主题。它只是一个警告,所以它不会破坏现有的代码。


如果你在代码中得到这个,你编写它时要么覆盖(如果那是你的实际意思)或者重命名你的方法,这样它就不会发生碰撞。


问候


Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


据说当你想要派生的

类中的成员隐藏基类中同名的成员时,你应该使用new:


A级

{

public int total;

}


B级:A

{

公共新浮动总数;

}


但是省略它只会导致警告,没有错误,并且该类似乎工作

s无论如何。它有什么实际意义吗?

If you find yourself thinking I need to use the new modifier in the code you''re writing a big red flashing light on the wall should start flashing with the text

WARNING! WARNING! DESIGN FLAW! DESIGN FLAW!

The new modifier is for handling the situation where your base class (which you are not in control of) introduces a member whose name collides with a member your derived class already has. In this situation the compiler warns you that you have a theoretical ambiguity (although what happens is well defined). It is asking you to specify for this collision whether you are intending to override or whether your method is completely separate from the base class version (the latter is what happens at runtime if you do nothing).

The C# compiler is asking you to be *explicit* about your intentions which a common theme in C#. Its only a warning so it doesn''t break existing code.

If you get this in code you are writing when you compile it either override (if that is what you actually meant) or rename your method so it doesn''t collide.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Supposedly you are supposed to use new when you want a member in a derived
class to hide a member of the same name in the base class:

class A
{
public int total;
}

class B:A
{
public new float total;
}

But omitting it causes only a warning, no error, and the class seems to work
the same either way. Does it have any real purpose?


>>>警告!警告!设计FLAW!设计FLAW!


亲爱的,我想我多年来一直都在做这件事。


或许不是。


这是我的设计,也是我对''新'的使用。你能提供另一种选择吗?


抽象类订单

{

OrderStatus状态; //其他内容

}

OrderStatus是一个枚举(自定义类 - 非原始枚举),包含New,Open,Closed。

基本上,订单知道其新的,开放的,关闭的以及如何管理业务

规则给出这些状态。所有订单都有这些状态,并且

可能更多。


订单来自SalesOrder,PurchaseOrder,WorkOrder,DeliveryOrder等等。


所以我有

公共类WorkOrder:订单

{

新的WorkOrderStatus状态; //不同[更聪明]状态对象

}



公共类WorkOrderStatus:OrderStatus

{

//更多状态,新方法,覆盖等

}


这是一个设计缺陷吗?多年来我一直在广泛使用这个模型,

非常高兴c#提供了''new''关键字。

" Richard Blewett [DevelopMentor] " < RI ****** @ NOSPAMdevelop.com>写在

消息新闻:eJ ************* @ TK2MSFTNGP09.phx.gbl ...
>>> WARNING! WARNING! DESIGN FLAW! DESIGN FLAW!

Oh dear, I guess I''ve been doing it all wrong for years now.

Or maybe not.

Here''s my design, and my use of ''New''. Could you provide another option?

Abstract Class Order
{
OrderStatus Status; //among other things
}
OrderStatus is an enum (custom class - not raw enum) with New,Open,Closed.
basically, an Order knows if its new,open,closed and how to manage business
rules given these statuses. ALL Orders will have these statuses, and
probably more.

From Order come SalesOrder,PurchaseOrder,WorkOrder,DeliveryOrder etc etc.

So I have
public class WorkOrder:Order
{
new WorkOrderStatus Status; //different [smarter] Status object
}
and
public class WorkOrderStatus:OrderStatus
{
//more statuses, new methods, overrides etc
}

Is that a design flaw? I''ve been using this model extensively for years,
and am extremely glad that c# provides the ''new'' keyword.
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:eJ*************@TK2MSFTNGP09.phx.gbl...
如果你发现自己在想我需要在代码中使用new修饰符
你在墙上写一个大红色闪烁灯应该开始闪烁

带文字
警告!警告!设计FLAW!设计FLAW!

新修饰符用于处理你的基类
(你无法控制)引入一个名字冲突的成员的情况

与派生类已有的成员。在这种情况下,编译器

警告你,你有一个理论上的模糊性(尽管发生的事情是

定义良好)。它要求你为这次碰撞指定你是否打算覆盖或者你的方法是否与基类版本完全分开(后者是发生在运行时如果你这样做什么没有什么)。
C#编译器要求你*明确*关于你的意图
这是C#中的一个共同主题。它只是一个警告,所以它不会破坏现有的

代码。
如果你在代码中得到这个,你在编译它时写的是
覆盖(如果这是什么你实际上是指)或重命名你的方法所以

不会碰撞。


Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

据说你是当你想要一个
派生类中的成员隐藏基类中同名的成员时,应该使用new:

A类
{
public总计;
}

B班:A
{公共新的总计;
}

但是省略它只引起警告,没有错误,并且类似乎
以相同的方式工作。它有什么实际用途吗?
If you find yourself thinking I need to use the new modifier in the code you''re writing a big red flashing light on the wall should start flashing
with the text
WARNING! WARNING! DESIGN FLAW! DESIGN FLAW!

The new modifier is for handling the situation where your base class (which you are not in control of) introduces a member whose name collides
with a member your derived class already has. In this situation the compiler
warns you that you have a theoretical ambiguity (although what happens is
well defined). It is asking you to specify for this collision whether you
are intending to override or whether your method is completely separate from
the base class version (the latter is what happens at runtime if you do
nothing).
The C# compiler is asking you to be *explicit* about your intentions which a common theme in C#. Its only a warning so it doesn''t break existing
code.
If you get this in code you are writing when you compile it either override (if that is what you actually meant) or rename your method so it
doesn''t collide.
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Supposedly you are supposed to use new when you want a member in a derived class to hide a member of the same name in the base class:

class A
{
public int total;
}

class B:A
{
public new float total;
}

But omitting it causes only a warning, no error, and the class seems to work the same either way. Does it have any real purpose?



这篇关于为什么要使用new修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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