新的与超越 [英] new versus override

查看:60
本文介绍了新的与超越的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释在会员声明中使用

''new''和'override'之间的差异所带来的影响。


我检查了MSDN,但它并不是为初学者编写的。例如

名称隐藏,它从这开始:实体的范围通常是

包含的程序文本多于
$ b的声明空间$ b entity。你听到的那种嗖嗖的声音是我的头脑!


SSG

Could someone explain the implications of the differences between using
''new'' and ''override'' on a member declaration, please.

I checked out MSDN but it''s not exactly written for the beginner. E.g.
on Name Hiding, it starts with this: "The scope of an entity typically
encompasses more program text than the declaration space of the
entity." That whizzing sound you''re hearing is it going over my head!

SSG

推荐答案

你好


假设你有一些课程:


A级

{

public A(){}


public virtual void Method1()

{

System.Diagnostics.Debug。 WriteLine(" A.Method1()");

}

public virtual void Method2()

{

System.Diagnostics.Debug.WriteLine(" A.Method2()");

}

}


class B:A

{

public B(){}


public override void Method1()

{

System.Diagnostics.Debug.WriteLine(" B.Method1()");

}

public new void Method2()

{

System.Diagnostics.Debug.WriteLine(" B.Method2()");

}

}

然后:


A a =新B();

a .Method1();

a.Method2();

所以,你会在输出窗口看到:

B.Method1()

A.Method2()


所以,如果你覆盖一些东西,如果你向下转换为

基类,它将是可访问的。但是如果你创建一个新的方法,你就无法在

下到基类之后访问它。


-

最诚挚的问候,

Andrew

http://www.codeproject.com/script/pr...asp?id=1181072

" ssg31415926" <是ne ********** @ gmail.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...
Hello

Suppose you have some classes:

class A
{
public A() { }

public virtual void Method1()
{
System.Diagnostics.Debug.WriteLine("A.Method1()");
}
public virtual void Method2()
{
System.Diagnostics.Debug.WriteLine("A.Method2()");
}
}

class B : A
{
public B() { }

public override void Method1()
{
System.Diagnostics.Debug.WriteLine("B.Method1()");
}
public new void Method2()
{
System.Diagnostics.Debug.WriteLine("B.Method2()");
}
}
and then:

A a = new B();
a.Method1();
a.Method2();
So, you will see in output window:
B.Method1()
A.Method2()

So, if you override something, it will be accessible if you down casting to
base class. But if you creating a new method, you can not access it after
down casting to base class.

--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"ssg31415926" <ne**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
有人可以解释在成员声明中使用
新和覆盖之间的差异所带来的影响。

我检查了MSDN但是它这不是为初学者写的。例如,在名称隐藏方面,它从以下开始:实体的范围通常包含比
实体的声明空间更多的程序文本。你听到的那种嗖嗖的声音是我的头脑!

SSG
Could someone explain the implications of the differences between using
''new'' and ''override'' on a member declaration, please.

I checked out MSDN but it''s not exactly written for the beginner. E.g.
on Name Hiding, it starts with this: "The scope of an entity typically
encompasses more program text than the declaration space of the
entity." That whizzing sound you''re hearing is it going over my head!

SSG



你好


假设你有一些课程:


A级

{

public A(){}


public virtual void Method1()

{

System.Diagnostics.Debug.WriteLine(" A.Method1()" ;);

}

public virtual void Method2()

{

System.Diagnostics.Debug.WriteLine (A.Method2());

}

}


B级:A

{

public B(){}


public override void Method1()

{

System.Diagnostics.Debug.WriteLine(" B.Method1()");

}

public new void Method2()

{

System.Diagnostics.Debug.WriteLine(" B.Method2()");

}

}

然后:


A a =新B();

a.Method1();

a.Method2();

所以,你会在输出窗口看到:

B.Method1()

A.Method2()


所以,如果你覆盖一些东西,那么如果你向下铸造到基础的
,它就是可以访问的。但是如果你创建一个新的方法,你就无法在

下到基类之后访问它。


-

最诚挚的问候,

Andrew

http://www.codeproject.com/script/pr...asp?id=1181072

" ssg31415926" <是ne ********** @ gmail.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...
Hello

Suppose you have some classes:

class A
{
public A() { }

public virtual void Method1()
{
System.Diagnostics.Debug.WriteLine("A.Method1()");
}
public virtual void Method2()
{
System.Diagnostics.Debug.WriteLine("A.Method2()");
}
}

class B : A
{
public B() { }

public override void Method1()
{
System.Diagnostics.Debug.WriteLine("B.Method1()");
}
public new void Method2()
{
System.Diagnostics.Debug.WriteLine("B.Method2()");
}
}
and then:

A a = new B();
a.Method1();
a.Method2();
So, you will see in output window:
B.Method1()
A.Method2()

So, if you override something, it will be accessible if you down casting to
base class. But if you creating a new method, you can not access it after
down casting to base class.

--
With best regards,
Andrew

http://www.codeproject.com/script/pr...asp?id=1181072
"ssg31415926" <ne**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
有人可以解释在成员声明中使用
新和覆盖之间的差异所带来的影响。

我检查了MSDN但是它这不是为初学者写的。例如,在名称隐藏方面,它从以下开始:实体的范围通常包含比
实体的声明空间更多的程序文本。你听到的那种嗖嗖的声音在我脑海中闪过!

SSG
Could someone explain the implications of the differences between using
''new'' and ''override'' on a member declaration, please.

I checked out MSDN but it''s not exactly written for the beginner. E.g.
on Name Hiding, it starts with this: "The scope of an entity typically
encompasses more program text than the declaration space of the
entity." That whizzing sound you''re hearing is it going over my head!

SSG



我可能是错的

两者都是关键字新建用于对象创建

并覆盖如果你想要覆盖继承中的方法


Shivprasad Koirala

C#,VB.NET,SQL SERVER,ASP.NET访谈问题
http://www.geocities.com/dotnetinterviews/

I can be wrong
Both are keywords New is for object creation
and overrides if you want to overirde a method in inheritance

Shivprasad Koirala
C# , VB.NET , SQL SERVER , ASP.NET Interview Questions
http://www.geocities.com/dotnetinterviews/


这篇关于新的与超越的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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