分享
 
 
 

10.7.1 Name hiding

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

10.7.1 Name hiding

The scope of an entity typically encompasses more program text than the

declaration space of the entity. In

particular, the scope of an entity may include declarations that introduce

new declaration spaces containing

entities of the same name. Such declarations cause the original entity to

become hidden. Conversely, an

entity is said to be visible when it is not hidden.

Name hiding occurs when scopes overlap through nesting and when scopes

overlap through inheritance. The

characteristics of the two types of hiding are described in the following

sections.

10.7.1.1 Hiding through nesting

Name hiding through nesting can occur as a result of nesting namespaces or

types within namespaces, as a

result of nesting types within classes or structs, and as a result of

parameter and local variable declarations.

[Example: In the example

class A

{

int i = 0;

void F() {

int i = 1;

}

void G() {

i = 1;

}

}

within the F method, the instance variable i is hidden by the local

variable i, but within the G method, i still

refers to the instance variable. end example]

C# LANGUAGE SPECIFICATION

82

When a name in an inner scope hides a name in an outer scope, it hides all

overloaded occurrences of that

name. [Example: In the example

class Outer

{

static void F(int i) {}

static void F(string s) {}

class Inner

{

void G() {

F(1); // Invokes Outer.Inner.F

F("Hello"); // Error

}

static void F(long l) {}

}

}

the call F(1) invokes the F declared in Inner because all outer occurrences

of F are hidden by the inner

declaration. For the same reason, the call F("Hello") results in a

compile-time error. end example]

10.7.1.2 Hiding through inheritance

Name hiding through inheritance occurs when classes or structs redeclare

names that were inherited from

base classes. This type of name hiding takes one of the following forms:

?A constant, field, property, event, or type introduced in a class or

struct hides all base class members

with the same name.

?A method introduced in a class or struct hides all non-method base class

members with the same name,

and all base class methods with the same signature (method name and

parameter count, modifiers, and

types).

?An indexer introduced in a class or struct hides all base class indexers

with the same signature

(parameter count and types).

The rules governing operator declarations (?7.9) make it impossible for a

derived class to declare an

operator with the same signature as an operator in a base class. Thus,

operators never hide one another.

Contrary to hiding a name from an outer scope, hiding an accessible name

from an inherited scope causes a

warning to be reported. [Example: In the example

class Base

{

public void F() {}

}

class Derived: Base

{

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

}

the declaration of F in Derived causes a warning to be reported. Hiding an

inherited name is specifically

not an error, since that would preclude separate evolution of base classes.

For example, the above situation

might have come about because a later version of Base introduced an F

method that wasn.t present in an

earlier version of the class. Had the above situation been an error, then

any change made to a base class in a

separately versioned class library could potentially cause derived classes

to become invalid. end example]

The warning caused by hiding an inherited name can be eliminated through

use of the new modifier:

[Example:

class Base

{

public void F() {}

}

Chapter 10 Basic concepts

83

class Derived: Base

{

new public void F() {}

}

The new modifier indicates that the F in Derived is .new., and that it is

indeed intended to hide the

inherited member. end example]

A declaration of a new member hides an inherited member only within the

scope of the new member.

[Example:

class Base

{

public static void F() {}

}

class Derived: Base

{

new private static void F() {} // Hides Base.F in Derived only

}

class MoreDerived: Derived

{

static void G() { F(); } // Invokes Base.F

}

In the example above, the declaration of F in Derived hides the F that was

inherited from Base, but since

the new F in Derived has private access, its scope does not extend to

MoreDerived. Thus, the call F() in

MoreDerived.G is valid and will invoke Base.F. 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- 王朝網路 版權所有