[ Pobierz całość w formacie PDF ]
" 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 (§10.2.1) are specifically not part of the declaration space of a class. 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).
10.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 constructors and destructors 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.
" Constructors and destructors are not inherited, but all other members are, regardless of their declared
accessibility (§3.3). However, depending on their declared accessibility, inherited members may not be
accessible in a derived class.
" A derived class can hide (§3.5.1.2) inherited members by declaring new members with the same name or
signature. Note however that hiding an inherited member does not remove the member it merely makes
the member inaccessible in the derived class.
" An instance of a class contains a copy of all instance fields declared in the class and its base classes, and an
implicit conversion (§6.1.4) exists from a derived class type to any of its base class types. Thus, a reference
to a derived class instance can be treated as a reference to a base class instance.
" 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 the function member is invoked.
10.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 §3.5.1.2.
Copyright Microsoft Corporation 1999-2000. All Rights Reserved. 167
C# LANGUAGE REFERENCE
If a new modifier is included in a declaration that doesn t hide an inherited member, a warning is issued to that
effect. This warning is suppressed by removing the new modifier.
It is an error to use the new and override modifiers in the same declaration.
10.2.3 Access modifiers
A class-member-declaration can have any one of the five possible types of declared accessibility (§3.3.1):
public, protected internal, protected, internal, or private. Except for the protected internal
combination, it is an error to specify more than one access modifier. When a class-member-declaration does not
include any access modifiers, the declaration defaults to private declared accessibility.
10.2.4 Constituent types
Types that are referenced in the declaration of a member are called the constituent types of the 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 constructor.
The constituent types of a member must be at least as accessible as the member itself (§3.3.4).
10.2.5 Static and instance members
Members of a class are either static members or instance members. Generally speaking, it is useful to think of
static members as belonging to classes and instance members as belonging to objects (instances of classes).
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 (§7.5.4) of the form E.M, E must denote a type. It is
an 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, indexer, operator, or constructor) does not operate on a specific
instance, and it is an error to refer to this in a static 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 (§7.5.4) of the form E.M, E must denote an
instance. It is an error for E to denote a type.
" Every instance of a class contains a separate copy of all instance fields of the class.
" An instance function member (method, property accessor, indexer accessor, constructor, or destructor)
operates on a given instance of the class, and this instance can be accessed as this (§7.5.7).
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
}
168 Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
Chapter 10 Classes
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 (§7.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 an error to
[ Pobierz całość w formacie PDF ]
© 2009 ...coÅ› siÄ™ w niej zmieniÅ‚o, zmieniÅ‚o i zmieniaÅ‚o nadal. - Ceske - Sjezdovky .cz. Design downloaded from free website templates