more about class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 instance class members...

44
More about Class 靜靜靜靜靜靜靜 靜靜靜靜靜靜 ©2011

Upload: linda-williamson

Post on 03-Jan-2016

360 views

Category:

Documents


0 download

TRANSCRIPT

More about Class靜宜大學資工系蔡奇偉副教授©2011

大綱

Instance Class MembersClass members can be associated with an instance of the class or with the class as a whole. By default, members are associated with an instance. You can think of each instance of a class as having its own copy of each class member. These members are called instance members.

Static Fields

• A static field is shared by all the instances of the class, and all the instances access the same memory location. Hence, if the value of the memory location is changed by one instance, the change is visible to all the instances.

• Use the static modifier to declare a field static.

Accessing Static Members from Outside the Class

Static members, like instance members, are also accessed from outside the class using dot-syntax notation. But since there is no instance, you must use the class name.

Lifetimes of Static Members

Static fields with no class instances can still be assigned to and read from, because the field is associated with the class, and not an instance.

Static Function Members

• Static function members, like static fields, are independent of any class instance. Even if there are no instances of a class, you can still call a static method.

• Static function members cannot access instance members. They can, however, access other static members.

Other Static Class Member Types

The types of class members that can be declared static are shown checked in the following table. The other member types cannot be declared static.

Member ConstantsMember constants are declared in the class declaration.

Constant fields act like static fields but do not have a storage location in memory.

Properties(屬性)A property is a member that represents an item of data in a class or class instance. Using a property appears very much like writing to, or reading from, a field. The syntax is the same.

範例

A property, like a field, has the following characteristics:

• It is a named class member.

• It has a type.

• It can be assigned to and read from.

Unlike a field, however, a property is a function member.

• It does not necessarily allocate memory for data

storage.

• It executes code

A property is a named set of two matching methods called accessors.

• The set accessor is used for assigning a value to the property.

• The get accessor is used for retrieving a value from the property.

範例

Property Declarations and Accessors

The set and get accessors have predefined syntax and semantics. You can think of the set accessor as a method with a single parameter that “sets” the value of the property. The get accessor has no parameters and returns the value the property.

• The set accessor always has the following:

A single, implicit value parameter named value, of the same type as the property

A return type of void

• The get accessor always has the following:

No parameters

A return type of the same type as the property

Using a Property• To write to a property, use the property’s name on the left side of an

assignment statement.

• To read from a property, use the property’s name in an expression.

範例

Read-Only and Write-Only PropertiesYou can leave one or the other (but not both) of a property’s accessors undefined by omitting its declaration.

• A property with only a get accessor is called a read-only property. A read-only property is a safe way of passing an item of data out from a class or class instance without allowing too much access.

• A property with only a set accessor is called a write-only property. A write-only property is a safe way of passing an item of data from outside the class to the class without allowing too much access.

• At least one of the two accessors must be defined, or the compiler will produce an error message.

範例

Automatically Implemented PropertiesC# 3.0 added automatically implemented properties, or auto-implemented properties, which allow you to just declare the property, without declaring a backing field. The compiler creates a hidden backing field for you and automatically hooks up the get and set accessors to it.

Static Properties

Instance ConstructorsAn instance constructor is a special method that is executed whenever a

new instance of a class is created.

• A constructor is used to initialize the state of the class instance.

• If you want to be able to create instances of your class from outside

the class, you need to declarethe constructor public.

• The name of the constructor is the same as the name of the class.

• A constructor cannot have a return value.

Default ConstructorsIf no instance constructor is explicitly supplied in the class declaration, then the compiler supplies an implicit, default constructor, which has the following characteristics:

• It takes no parameters.

• It has an empty body.

If you declare any constructors at all for a class, then the compiler does not define a default constructor for the class.

Static Constructors• There can only be a single static constructor for a class, and it cannot

have parameters.

• Static constructors cannot have accessibility modifiers.

• You cannot explicitly call static constructors from your program. They’re called automatically by the system, at some time:

- Before any instance of the class is created

- Before any static member of the class is referenced

Object Initializers

An object initializer extends that syntax by placing a list of member initializations at the end of the expression. This allows you to set the values of fields and properties when creating a new instance of an object.

DestructorsDestructors perform actions required to clean up or release unmanaged resources after an instance of a class is no longer referenced.

• You can have only a single destructor per class.

• A destructor cannot have parameters.

• A destructor cannot have accessibility modifiers.

• A destructor has the same name as the class but is preceded by a tilde character (pronounced TIL-duh).

• A destructor only acts on instances of classes; hence, there are no static destructors.

• You cannot call a destructor explicitly in your code. Instead, it is called during the garbage collection process, when the garbage collector analyzes your code and determines that there is no longer any path through your code that references the object.

• Don’t implement a destructor if you don’t need one. They can incur performance costs.

• A destructor should only release external resources that the object owns.

• A destructor should not access other objects because you can’t assume that those objects haven’t already been destroyed.

The readonly Modifier

The this KeywordThe this keyword, used in a class, is a reference to the current instance.

Indexers

There are times, however, when it would be convenient to be able to access them with an index, as if the instance were an array of fields. This is exactly what indexers allow you to do.

An indexer is a pair of get and set accessors, similar to those of properties. The following figure shows the representation of an indexer for a class that can get and set values of type string.

Indexer Overloading

Partial Classes and Partial Types

Partial Methods