分享
 
 
 

17.2 Class members

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

The members of a class consist of the members introduced by its

class-member-declarations and the members

inherited from the direct base class.

class-member-declarations:

class-member-declaration

class-member-declarations class-member-declaration

class-member-declaration:

constant-declaration

field-declaration

method-declaration

property-declaration

event-declaration

indexer-declaration

operator-declaration

constructor-declaration

destructor-declaration

static-constructor-declaration

type-declaration

The members of a class are divided into the following categories:

? Constants, which represent constant values associated with that class (§17

.3).

? Fields, which are the variables of that class (§17.4).

? Methods, which implement the computations and actions that can be

performed by that class (§17.5).

? Properties, which define named characteristics and the actions associated

with reading and writing those

characteristics (§17.6).

? Events, which define notifications that can be generated by that class (§1

7.7).

? Indexers, which permit instances of that class to be indexed in the same

way as arrays (§17.8).

? Operators, which define the expression operators that can be applied to

instances of that class (§17.9).

? Instance constructors, which implement the actions required to initialize

instances of that class (§17.10)

? Destructors, which implement the actions to be performed before instances

of that class are permanently

discarded (§17.12).

? Static constructors, which implement the actions required to initialize

that class itself (§17.11).

? Types, which represent the types that are local to that class (§16.5).

Members that can contain executable code are collectively known as the

function members of the class. The

function members of a class are the methods, properties, events, indexers,

operators, instance constructors,

destructors, and static constructors of that class.

A class-declaration creates a new declaration space (§10.3), and the

class-member-declarations immediately

contained by the class-declaration introduce new members into this

declaration space. The following rules apply

to class-member-declarations:

? Instance constructors, destructors, and static constructors must have the

same name as the immediately

enclosing class. All other members must have names that differ from the

name of the immediately enclosing

class.

? The name of a constant, field, property, event, or type must differ from

the names of all other members

declared in the same class.

? The name of a method must differ from the names of all other non-methods

declared in the same class. In

addition, the signature (§10.6) of a method must differ from the

signatures of all other methods declared in

Chapter 17 Classes

213

the same class, and two methods declared in the same class may not have

signatures that differ solely by ref

and out.

? The signature of an instance constructor must differ from the signatures

of all other instance constructors

declared in the same class, and two constructors declared in the same class

may not have signatures that

differ solely by ref and out.

? The signature of an indexer must differ from the signatures of all other

indexers declared in the same class.

? The signature of an operator must differ from the signatures of all other

operators declared in the same class.

The inherited members of a class (§17.2.1) are not part of the declaration

space of a class. [Note: Thus, a derived

class is allowed to declare a member with the same name or signature as an

inherited member (which in effect

hides the inherited member). end note]

17.2.1 Inheritance

A class inherits the members of its direct base class. Inheritance means

that a class implicitly contains all

members of its direct base class, except for the instance constructors,

destructors, and static constructors of the

base class. Some important aspects of inheritance are:

? Inheritance is transitive. If C is derived from B, and B is derived from

A, then C inherits the members

declared in B as well as the members declared in A.

? A derived class extends its direct base class. A derived class can add

new members to those it inherits, but it

cannot remove the definition of an inherited member.

? Instance constructors, destructors, and static constructors are not

inherited, but all other members are,

regardless of their declared accessibility (§10.5). However, depending on

their declared accessibility,

inherited members might not be accessible in a derived class.

? A derived class can hide (§10.7.1.2) inherited members by declaring new

members with the same name or

signature. However, hiding an inherited member does not remove that

member?it merely makes that

member inaccessible in the derived class.

? An instance of a class contains a set of all instance fields declared in

the class and its base classes, and an

implicit conversion (§13.1.4) exists from a derived class type to any of

its base class types. Thus, a reference

to an instance of some derived class can be treated as a reference to an

instance of any of its base classes.

? A class can declare virtual methods, properties, and indexers, and

derived classes can override the

implementation of these function members. This enables classes to exhibit

polymorphic behavior wherein the

actions performed by a function member invocation varies depending on the

run-time type of the instance

through which that function member is invoked.

17.2.2 The new modifier

A class-member-declaration is permitted to declare a member with the same

name or signature as an inherited

member. When this occurs, the derived class member is said to hide the base

class member. Hiding an inherited

member is not considered an error, but it does cause the compiler to issue

a warning. To suppress the warning,

the declaration of the derived class member can include a new modifier to

indicate that the derived member is

intended to hide the base member. This topic is discussed further in §10.7.1

.2.

If a new modifier is included in a declaration that doesn?t hide an

inherited member, a warning to that effect is

issued. This warning is suppressed by removing the new modifier.

17.2.3 Access modifiers

A class-member-declaration can have any one of the five possible kinds of

declared accessibility (§10.5.1):

public, protected internal, protected, internal, or private. Except for the

protected internal

combination, it is a compile-time error to specify more than one access

modifier. When a class-memberdeclaration

does not include any access modifiers, private is assumed.

C# LANGUAGE SPECIFICATION

214

17.2.4 Constituent types

Types that are used in the declaration of a member are called the

constituent types of that member. Possible

constituent types are the type of a constant, field, property, event, or

indexer, the return type of a method or

operator, and the parameter types of a method, indexer, operator, or

instance constructor. The constituent types of

a member must be at least as accessible as that member itself (§10.5.4).

17.2.5 Static and instance members

Members of a class are either static members or instance members. [Note:

Generally speaking, it is useful to

think of static members as belonging to classes and instance members as

belonging to objects (instances of

classes). end note]

When a field, method, property, event, operator, or constructor declaration

includes a static modifier, it

declares a static member. In addition, a constant or type declaration

implicitly declares a static member. Static

members have the following characteristics:

? When a static member is referenced in a member-access (§14.5.4) of the

form E.M, E must denote a type that

has a member M. It is a compile-time error for E to denote an instance.

? A static field identifies exactly one storage location. No matter how

many instances of a class are created,

there is only ever one copy of a static field.

? A static function member (method, property, event, operator, or

constructor) does not operate on a specific

instance, and it is a compile-time error to refer to this in such a

function member.

When a field, method, property, event, indexer, constructor, or destructor

declaration does not include a static

modifier, it declares an instance member. (An instance member is sometimes

called a non-static member.)

Instance members have the following characteristics:

? When an instance member is referenced in a member-access (§14.5.4) of

the form E.M, E must denote an

instance of a type that has a member M. It is a compile-time error for E to

denote a type.

? Every instance of a class contains a separate set of all instance fields

of the class.

? An instance function member (method, property, indexer, instance

constructor, or destructor) operates on a

given instance of the class, and this instance can be accessed as this (§14.

5.7).

[Example: The following example illustrates the rules for accessing static

and instance members:

class Test

{

int x;

static int y;

void F() {

x = 1; // Ok, same as this.x = 1

y = 1; // Ok, same as Test.y = 1

}

static void G() {

x = 1; // Error, cannot access this.x

y = 1; // Ok, same as Test.y = 1

}

static void Main() {

Test t = new Test();

t.x = 1; // Ok

t.y = 1; // Error, cannot access static member through instance

Test.x = 1; // Error, cannot access instance member through type

Test.y = 1; // Ok

}

}

The F method shows that in an instance function member, a simple-name (§14.5

.2) can be used to access both

instance members and static members. The G method shows that in a static

function member, it is a compile-time

error to access an instance member through a simple-name. The Main method

shows that in a member-access

Chapter 17 Classes

215

(§14.5.4), instance members must be accessed through instances, and static

members must be accessed through

types. end example]

17.2.6 Nested types

A type declared within a class or struct is called a nested type. A type

that is declared within a compilation unit

or namespace is called a non-nested type. [Example: In the following

example:

using System;

class A

{

class B

{

static void F() {

Console.WriteLine("A.B.F");

}

}

}

class B is a nested type because it is declared within class A, and class A

is a non-nested type because it is

declared within a compilation unit. end example]

17.2.6.1 Fully qualified name

The fully qualified name (§10.8.1) for a nested type is S.N where S is the

fully qualified name of the type in

which type N is declared.

17.2.6.2 Declared accessibility

Non-nested types can have public or internal declared accessibility and

they have internal declared accessibility

by default. Nested types can have these forms of declared accessibility

too, plus one or more additional forms of

declared accessibility, depending on whether the containing type is a class

or struct:

? A nested type that is declared in a class can have any of five forms of

declared accessibility (public, protected

internal, protected, internal, or private) and, like other class members,

defaults to private declared

accessibility.

? A nested type that is declared in a struct can have any of three forms of

declared accessibility (public,

internal, or private) and, like other struct members, defaults to private

declared accessibility.

[Example: The example

public class List

{

// Private data structure

private class Node

{

public object Data;

public Node Next;

public Node(object data, Node next) {

this.Data = data;

this.Next = next;

}

}

private Node first = null;

private Node last = null;

// Public interface

public void AddToFront(object o) {?}

public void AddToBack(object o) {?}

public object RemoveFromFront() {?}

public object AddToFront() {?}

public int Count { get {?} }

}

declares a private nested class Node. end example]

C# LANGUAGE SPECIFICATION

216

17.2.6.3 Hiding

A nested type may hide (§10.7.1.1) a base member. The new modifier is

permitted on nested type declarations so

that hiding can be expressed explicitly. [Example: The example

using System;

class Base

{

public static void M() {

Console.WriteLine("Base.M");

}

}

class Derived: Base

{

new public class M

{

public static void F() {

Console.WriteLine("Derived.M.F");

}

}

}

class Test

{

static void Main() {

Derived.M.F();

}

}

shows a nested class M that hides the method M defined in Base. end example]

17.2.6.4 this access

A nested type and its containing type do not have a special relationship

with regard to this-access (§14.5.7).

Specifically, this within a nested type cannot be used to refer to instance

members of the containing type. In

cases where a nested type needs access to the instance members of its

containing type, access can be provided by

providing the this for the instance of the containing type as a constructor

argument for the nested type.

[Example: The following example

using System;

class C

{

int i = 123;

public void F() {

Nested n = new Nested(this);

n.G();

}

public class Nested

{

C this_c;

public Nested(C c) {

this_c = c;

}

public void G() {

Console.WriteLine(this_c.i);

}

}

}

class Test

{

static void Main() {

C c = new C();

c.F();

}

}

Chapter 17 Classes

217

shows this technique. An instance of C creates an instance of Nested, and

passes its own this to Nested’s

constructor in order to provide subsequent access to C’s instance members.

end example]

17.2.6.5 Access to private and protected members of the containing type

A nested type has access to all of the members that are accessible to its

containing type, including members of the

containing type that have private and protected declared accessibility.

[Example: The example

using System;

class C

{

private static void F() {

Console.WriteLine("C.F");

}

public class Nested

{

public static void G() {

F();

}

}

}

class Test

{

static void Main() {

C.Nested.G();

}

}

shows a class C that contains a nested class Nested. Within Nested, the

method G calls the static method F

defined in C, and F has private declared accessibility. end example]

A nested type also may access protected members defined in a base type of

its containing type. [Example: In the

example

using System;

class Base

{

protected void F() {

Console.WriteLine("Base.F");

}

}

class Derived: Base

{

public class Nested

{

public void G() {

Derived d = new Derived();

d.F(); // ok

}

}

}

class Test

{

static void Main() {

Derived.Nested n = new Derived.Nested();

n.G();

}

}

the nested class Derived.Nested accesses the protected method F defined in

Derived’s base class, Base, by

calling through an instance of Derived. end example]

17.2.7 Reserved member names

To facilitate the underlying C# runtime implementation, for each source

member declaration that is a property,

event, or indexer, the implementation must reserve two method signatures

based on the kind of the member

C# LANGUAGE SPECIFICATION

218

declaration, its name, and its type (§17.2.7.1, §17.2.7.2, §17.2.7.3).

It is a compile-time error for a program to

declare a member whose signature matches one of these reserved signatures,

even if the underlying runtime

implementation does not make use of these reservations.

The reserved names do not introduce declarations, thus they do not

participate in member lookup. However, a

declaration?s associated reserved method signatures do participate in

inheritance (§17.2.1), and can be hidden

with the new modifier (§17.2.2).

[Note: The reservation of these names serves three purposes:

1. To allow the underlying implementation to use an ordinary identifier as

a method name for get or set

access to the C# language feature.

2. To allow other languages to interoperate using an ordinary identifier as

a method name for get or set

access to the C# language feature.

3. To help ensure that the source accepted by one conforming compiler is

accepted by another, by making

the specifics of reserved member names consistent across all C#

implementations.

end note]

The declaration of a destructor (§17.12) also causes a signature to be

reserved (§17.2.7.4).

17.2.7.1 Member Names Reserved for Properties

For a property P (§17.6) of type T, the following signatures are reserved:

T get_P();

void set_P(T value);

Both signatures are reserved, even if the property is read-only or

write-only.

[Example: In the example

using System;

class A

{

public int P {

get { return 123; }

}

}

class B: A

{

new public int get_P() {

return 456;

}

new public void set_P(int value) {

}

}

class Test

{

static void Main() {

B b = new B();

A a = b;

Console.WriteLine(a.P);

Console.WriteLine(b.P);

Console.WriteLine(b.get_P());

}

}

a class A defines a read-only property P, thus reserving signatures for

get_P and set_P methods. A class B

derives from A and hides both of these reserved signatures. The example

produces the output:

123

123

456

end example]

Chapter 17 Classes

219

17.2.7.2 Member Names Reserved for Events

For an event E (§17.7) of delegate type T, the following signatures are

reserved:

void add_E(T handler);

void remove_E(T handler);

17.2.7.3 Member Names Reserved for Indexers

For an indexer (§17.8) of type T with parameter-list L, the following

signatures are reserved:

T get_Item(L);

void set_Item(L, T value);

Both signatures are reserved, even if the indexer is read-only or

write-only.

17.2.7.4 Member Names Reserved for Destructors

For a class containing a destructor (§17.12), the following signature is

reserved:

void Finalize();

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
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- 王朝網路 版權所有