new vs override - 一个实际的例子 [英] new vs override - a practical example

查看:47
本文介绍了new vs override - 一个实际的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人遇到过使用新的需要?在一个方法

声明(打破继承/版本控制)?虽然我知道

它做了什么以及如何使用我真的很难为它构建一个有用的现实生活场景。也许你可以从现实生活中给出一个实际的例子吗?

Did anyone EVER come across the need to use "new" in a method
declaration (breaking with inheritance / versioning)? Allthough I know
what it does and how it is used I really have a hard time to make up a
usefull real life scenario for it. Maybe you can give a practical
example from a real life?

推荐答案

让我们说你继承自你没写过的课。它有一个Clear

方法,不会被声明为允许它被覆盖。所以没有

方式来覆盖它。你必须编写一个名为Clear2

或其他东西的方法。

所以,你只需使用''new''就可以拥有自己的Clear方法。 />
调用该对象。这也意味着你不能在没有投射到基类的情况下调用原始版本




" bonk" < SC ****************** @ gmx.de>在消息中写道

新闻:关于************** @ TK2MSFTNGP10.phx.gbl ...
Let''s say you are inheriting from a class you did not write. It has a Clear
method that is not declared as allowing it to be overridden. So there is no
way for you to override it. You would have to write a method called Clear2
or something.
So, instead you use ''new'' to just have your own Clear method that can be
called on that object. That also means you can''t call the original version
without casting to the base class.

"bonk" <sc******************@gmx.de> wrote in message
news:On**************@TK2MSFTNGP10.phx.gbl...
有没有人偶然遇到过是否需要使用新在方法声明中
(打破继承/版本控制)?尽管我知道它的作用以及如何使用它我真的很难为它构建一个有用的真实生活场景。也许你可以从一个真实的生活中给出一个实际的例子?
Did anyone EVER come across the need to use "new" in a method declaration
(breaking with inheritance / versioning)? Allthough I know what it does
and how it is used I really have a hard time to make up a usefull real
life scenario for it. Maybe you can give a practical example from a real
life?



Marina schrieb:
Marina schrieb:
让's'说你是从一个你没写过的课继承的。它有一个Clear
方法,不会被声明为允许它被覆盖。所以你没有覆盖它的方法。你必须编写一个名为Clear2
的方法。
所以,你可以使用''new''来拥有自己的Clear方法,可以在该对象上调用它。这也意味着你不能在没有投射到基类的情况下调用原始版本。

bonk < SC ****************** @ gmx.de>在消息中写道
新闻:关于************** @ TK2MSFTNGP10.phx.gbl ...
Let''s say you are inheriting from a class you did not write. It has a Clear
method that is not declared as allowing it to be overridden. So there is no
way for you to override it. You would have to write a method called Clear2
or something.
So, instead you use ''new'' to just have your own Clear method that can be
called on that object. That also means you can''t call the original version
without casting to the base class.

"bonk" <sc******************@gmx.de> wrote in message
news:On**************@TK2MSFTNGP10.phx.gbl...
有没有人遇到过是否需要使用新在方法声明中
(打破继承/版本控制)?尽管我知道它的作用以及如何使用它我真的很难为它构建一个有用的真实生活场景。也许你可以从一个真实的生活中给出一个实际的例子?
Did anyone EVER come across the need to use "new" in a method declaration
(breaking with inheritance / versioning)? Allthough I know what it does
and how it is used I really have a hard time to make up a usefull real
life scenario for it. Maybe you can give a practical example from a real
life?




对不起,这不是我想要的,也许我应该更具体b / b
具体。我很清楚,我必须使用新字母。重新定义一个在声明中没有虚拟的物品。当我更喜欢新时,我更期待

的实际例子。 INSTEAD

" override"隐藏我派生自的基类的VIRTUAL方法:


A级

{

public virtual void DoIt(){ }

}

B级:A

{

public / *覆盖(或)new(带或没有)virtual * / void DoIt(){}

}

class C:B

{

public / *覆盖(或)新* / void DoIt()

}


Sorry, that was not quite what I was looking for, maybe I should be more
specific. It is clear to me that I have to use "new" to redefine a
mehtod that doesn''t have virtual in its declaration. I was more looking
for a practical example for when I would prefer "new" INSTEAD of
"override" to hide the VIRTUAL mehtod of the baseclass I derived from:

class A
{
public virtual void DoIt(){}
}
class B : A
{
public /*override (or) new (with or without) virtual*/ void DoIt() {}
}
class C : B
{
public /*override (or) new*/ void DoIt()
}


bonk写道:
Marina schrieb :
Marina schrieb:
让我们说你是从一个你没写过的课继承的。它有一个
Clear方法,不会被声明为允许它被覆盖。所以
你无法覆盖它。您必须编写一个名为Clear2的方法。
因此,您只需使用新来拥有自己的Clear方法即可在该对象上调用。这也意味着你不能在没有投入基础类的情况下调用原始的
版本。

bonk < SC ****************** @ gmx.de>在消息中写道
新闻:关于************** @ TK2MSFTNGP10.phx.gbl ...
Let''s say you are inheriting from a class you did not write. It has a
Clear method that is not declared as allowing it to be overridden. So
there is no way for you to override it. You would have to write a
method called Clear2 or something.
So, instead you use ''new'' to just have your own Clear method that can
be called on that object. That also means you can''t call the original
version without casting to the base class.

"bonk" <sc******************@gmx.de> wrote in message
news:On**************@TK2MSFTNGP10.phx.gbl...
有没有人遇到过是否需要使用新在一个方法
声明(打破继承/版本控制)?尽管我知道它的作用以及如何使用它我真的很难为它构建一个有用的现实生活场景。也许你可以从现实生活中给出一个实际的例子?
Did anyone EVER come across the need to use "new" in a method
declaration (breaking with inheritance / versioning)? Allthough I
know what it does and how it is used I really have a hard time to
make up a usefull real life scenario for it. Maybe you can give a
practical example from a real life?



抱歉,这不是我想要的,也许我应该更多<特定的。我很清楚,我必须使用新字母。重新定义一个在宣言中没有虚拟的方法。当我更喜欢新时,我更多地寻找一个实际的例子。 INSTEAD
覆盖隐藏我从以下基础课程中得到的基础类的虚拟方法:

{/ c>公共虚拟空洞DoIt(){}
}
B类:A
公共/ *覆盖(或)新(有或没有)虚拟* / void DoIt(){}
}
C类:B
{
public / * override(或)new * / void DoIt()
}


Sorry, that was not quite what I was looking for, maybe I should be more
specific. It is clear to me that I have to use "new" to redefine a
mehtod that doesn''t have virtual in its declaration. I was more looking
for a practical example for when I would prefer "new" INSTEAD of
"override" to hide the VIRTUAL mehtod of the baseclass I derived from:

class A
{
public virtual void DoIt(){}
}
class B : A
{
public /*override (or) new (with or without) virtual*/ void DoIt() {}
}
class C : B
{
public /*override (or) new*/ void DoIt()
}



我不时使用它来使我的coode更具可读性:


class AImp {}

class BImp:AImp {}


A级

{

保护AImp Pimpl {get; }

}


b使用BImp

等级B:A

{

private new BImp Pimpl {get; }

}


所以写入B内部((BImp)Pimpl).DoIt(),我可以使用

Pimpl.DoIt();


HTH,

Andy

----

要直接给我发电子邮件,请删除下面的N * o * SPA * M部分:

xmenN*o*SPA*M40@gmN*o*SPA*Mx.net


I use it from time to time to make my coode more readable:

class AImp{}
class BImp: AImp{}

class A
{
protected AImp Pimpl{ get; }
}

b uses a BImp
class B : A
{
private new BImp Pimpl{ get; }
}

so inside of B intead of writing ((BImp)Pimpl).DoIt(), I can use
Pimpl.DoIt();

HTH,
Andy
----
To email me directly, remove the N*o*SPA*M part below:
xmenN*o*SPA*M40@gmN*o*SPA*Mx.net


这篇关于new vs override - 一个实际的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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