`
talin2010
  • 浏览: 501444 次
  • 性别: Icon_minigender_1
  • 来自: 河北
社区版块
存档分类
最新评论

17.2 Class members

阅读更多
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();
分享到:
评论

相关推荐

    C# 语言规格说明(English Edition第五版)

    3.4.4 Class members 59 3.4.5 Interface members 60 3.4.6 Array members 60 3.4.7 Delegate members 60 3.5 Member access 60 3.5.1 Declared accessibility 60 3.5.2 Accessibility domains 61 3.5.3 Protected ...

    C++标准(Standard for Programming Language C++)

    17.2 The C standard library 17.3 Definitions 17.4 Additional definitions 17.5 Method of description (Informative) 17.6 Library-wide requirements 18 Language support library 18.1 General 18.2 ...

    python3.6.5参考手册 chm

    PEP 487: Simpler customization of class creation PEP 487: Descriptor Protocol Enhancements PEP 519: Adding a file system path protocol PEP 495: Local Time Disambiguation PEP 529: Change Windows ...

    Python Cookbook英文版

    5.2 Getting All Members of a Class Hierarchy 5.3 Calling a Superclass _ _init_ _ Method if It Exists 5.4 Calling a Superclass Implementation of a Method 5.5 Implementing Properties 5.6 ...

    McGraw C++程序调试实用手册

    4.5.1 List Members 4.5.2 Type Info 4.5.3 Parameter Information 4.5.4 Complete Word 4.5.5 Go Definition/Refefence 4.5.6 Go To Disassembly 4.5.7 Insert/Remove Breakpoint。 4.6 Debugger...

    Scala for the Impatient 2nd (完整英文第二版 带书签)

    7.9 Renaming and Hiding Members 86 7.10 Implicit Imports 86 Exercises 87 ix Contents8 INHERITANCE A1 91 8.1 Extending a Class 91 8.2 Overriding Methods 92 8.3 Type Checks and Casts 93 8.4 Protected ...

    Windows2000活动目录开发人员参考库第4卷ADSI参考手册

    5.2.5 ADS_CLASS_DEF结构 5.2.6 ADS_DN_WITH_BINART结构 5.2.7 ADS_DN_WITH_STRING结构 5.2.8 ADS_EMAIL结构 5.2.9 ADS_FAXNUMBER结构 5.2.10 ADS_HOLD结构 5.2.11 ADS_NETADDRESS结构 5.2.12 ADS_NT_...

    Google C++ International Standard.pdf

    Contents Contents ii List of Tables x List of Figures xiv 1 Scope 1 2 Normative references 2 3 Terms and definitions 3 4 General principles 7 4.1 Implementation compliance . ....4.2 Structure of this ...

    C++ 标准 ISO 14882-2011

    Contents Contents iii List of Tables xi List of Figures xv 1 General 1 1.1 Scope . . . . ....1.2 Normative references ....1.3 Terms and definitions ....1.4 Implementation compliance ....1.5 Structure of this ...

    C++标准库(第二版)英文版.pdf

    4.3.3 PassingExceptions with Classexception_ptr . . . . . . . . . . . . . . 52 4.3.4 Throwing Standard Exceptions . . . . . . . . . . . . . . . . . . . . . . . 53 4.3.5 Deriving from Standard ...

    SystemVerilog Reference Manual 3.1a(中英文版)+最新SV IEEE 标准

    3.12 Class...............................................................................................................................................26 3.13 Singular and aggregate types .............

Global site tag (gtag.js) - Google Analytics