分享
 
 
 

17.5.4 Override methods

王朝other·作者佚名  2006-05-09
窄屏简体版  字體: |||超大  

When an instance method declaration includes an override modifier, the

method is said to be an override

method. An override method overrides an inherited virtual method with the

same signature. Whereas a virtual

method declaration introduces a new method, an override method declaration

specializes an existing inherited

virtual method by providing a new implementation of that method.

The method overridden by an override declaration is known as the overridden

base method. For an override

method M declared in a class C, the overridden base method is determined by

examining each base class of C,

starting with the direct base class of C and continuing with each

successive direct base class, until an accessible

method with the same signature as M is located. For the purposes of

locating the overridden base method, a

method is considered accessible if it is public, if it is protected, if it

is protected internal, or if it is

internal and declared in the same program as C.

A compile-time error occurs unless all of the following are true for an

override declaration:

? An overridden base method can be located as described above.

? The overridden base method is a virtual, abstract, or override method. In

other words, the overridden base

method cannot be static or non-virtual.

? The overridden base method is not a sealed method.

? The override declaration and the overridden base method have the same

return type.

? The override declaration and the overridden base method have the same

declared accessibility. In other

words, an override declaration cannot change the accessibility of the

virtual method.

An override declaration can access the overridden base method using a

base-access (§14.5.8). [Example: In the

example

class A

{

int x;

public virtual void PrintFields() {

Console.WriteLine("x = {0}", x);

}

}

C# LANGUAGE SPECIFICATION

236

class B: A

{

int y;

public override void PrintFields() {

base.PrintFields();

Console.WriteLine("y = {0}", y);

}

}

the base.PrintFields() invocation in B invokes the PrintFields method

declared in A. A base-access

disables the virtual invocation mechanism and simply treats the base method

as a non-virtual method. Had the

invocation in B been written ((A)this).PrintFields(), it would recursively

invoke the PrintFields

method declared in B, not the one declared in A, since PrintFields is

virtual and the run-time type of

((A)this) is B. end example]

Only by including an override modifier can a method override another

method. In all other cases, a method

with the same signature as an inherited method simply hides the inherited

method. [Example: In the example

class A

{

public virtual void F() {}

}

class B: A

{

public virtual void F() {} // Warning, hiding inherited F()

}

the F method in B does not include an override modifier and therefore does

not override the F method in A.

Rather, the F method in B hides the method in A, and a warning is reported

because the declaration does not

include a new modifier. end example]

[Example: In the example

class A

{

public virtual void F() {}

}

class B: A

{

new private void F() {} // Hides A.F within B

}

class C: B

{

public override void F() {} // Ok, overrides A.F

}

the F method in B hides the virtual F method inherited from A. Since the

new F in B has private access, its scope

only includes the class body of B and does not extend to C. Therefore, the

declaration of F in C is permitted to

override the F inherited from A. end example]

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有