|
|
|
SummaryIn this chapter, you learned the different types of arrays and collections that are provided in the .NET Framework. You also learned how to create your own strongly typed collection of addresses. You can now apply the knowledge that you learned to determine which array or collection to use and, if necessary, create your own custom collection. |
|
|
|
|
|
|
Further Reading
Programming C# , Third Edition by Jesse Liberty, O'Reilly. ISBN: 0596004893. The Applied Microsoft .NET Framework Programming in C# Collection , Microsoft Press. ISBN: 0735619751. |
|
|
|
|
|
|
Chapter 6. Objects and ClassesIN BRIEF
This chapter will explain and
WHAT YOU NEED
OBJECTS AND CLASSES AT A GLANCE
|
||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
|
Objects and Classes
It might not seem like it, but object-oriented
concepts have been around for nearly 30
Class AttributesThis next section will show you what attributes are, and how they carry over from the design board into code as properties and fields. Definition
The definition of
attribute
varies slightly depending on whether
you consult a dictionary or whether you
Description of an Attribute
An
attribute
is
a physical characteristic of a class or object (the terms
class
and
object
are defined later in this section).
That characteristic can be something as simple as the color of the
object or as complex as a contained object or class. Unlike a class
or object, an attribute has no identity outside of the class or
object that contains it. For example, the color red, outside of a
representation of a bicycle, does not have an identity. However, as
part of an instance of a bicycle, the
Design and UML Notation of AttributesIn UML, an attribute begins with a lowercase letter. This type of capitalization is sometimes referred to as camel case . An attribute is also prefaced by a scope indicator:
+
,
-
, or
#
. Each
symbol corresponds to an appropriate scoping type: public, private,
and protected, respectively. Figure 6.1 shows four private
attributes inside a class definition. Although public is a valid
scoping operator for attributes, it is
Figure 6.1. In UML, attributes are
listed in the second
|
|
UML Notation |
Visual C# Declaration |
|---|---|
|
-birthDate |
private DateTime m_birthDate; |
|
-hairColor |
private string hairColor = "blue"; |
|
#lastName |
protected string m_lastName = "Kruger"; |
|
#firstName |
protected string m_firstName = "Lonny"; |
|
+badAttribute |
public string m_badAttribute = "should never use public attributes"; |
Where you can think of attributes as
characteristics that can be represented with nouns (age, height,
weight, sex, and so on), operations are actions that an object
An operation is a service exposed by a class or
object. When an object receives a message from another object, the
receiving object
In UML, an operation
Name(argument : ArgumentType = DefaultValue, ...):
ReturnType {PropertyValues} {Constraints}
In Visual C# .NET, an operation takes the following form:
Attributes Modifiers ReturnType Name(ParameterList)
{
}
The examples in Table 6.2 show attributes and their corresponding declarations in Visual C# .NET.
|
UML Notation |
Visual C# .NET Declaration |
|---|---|
|
-InternalOpen() |
private void InternalOpen() |
|
#SetFirstName(firstName:string) |
protected void SetFirstName (string firstName) |
|
#GetLastName():string |
protected string GetLastName() |
|
+
|
public void Open() |
Looking at the real world, it is difficult to
think of anything that cannot be represented as a class. Everywhere
you look is an instance of a class. For example, a bird, a dog, a
cat, a cow, and a person can all be
There are three main things that make up a class definition: attributes, operations, and constraints. As defined earlier, attributes are characteristics of a class or object; operations are actions that class or object can perform; and constraints are parameters to which the class or object must conform. For example, a dog can be represented as a class with certain attributes, such as legs, hair, mouth, and teeth. It can also have operations, such as walk and speak. Finally, it can also have a constraint, which states that if a person calls the dog, it must go to that person.
In UML, a class is depicted by a rectangle, as
shown in Figure 6.3. An object has three compartments: name,
attributes, and operations. The first compartment is for the
object's name and stereotype. A stereotype is
Figure 6.4 shows the class definition for Door . It can be read as a Door has attributes of color , doorType (interior or exterior), and isClosed (opened or closed). In addition, the operations Open and Close can be performed.
An object is an instance of a class. However, it is easier to think of an object as a real-world thing, such as an apple, a person, a cow, and so on. An object contains state information that is defined in the class structure. For example, in the case of the Door class described in the class example, an object representation would be the actual door that you step through. It has a clearly defined state, such as brown , open , and exterior . An object knows which messages it receives have a corresponding operation and appropriately invokes that operation when received.
Similar to the class representation, an object
is depicted by a rectangle, as shown in Figure 6.5. The rectangle
contains either the name of the object or the name of the object
and the corresponding class name. The name of the object usually
begins with a lowercase letter and is
When attributes are displayed, the rectangle is divided into two separate compartments. They are displayed with the attribute name followed by a sample value or the value of that attribute in the current context. Because operations are not contextual, they are not shown in the object representation.
Figure 6.6 shows an instance of class Point named aPoint . It has two attributes: X and Y . In the current context, the values of X and Y are 200 and 100 , respectively.
Each object or instance of a class that has at least one attribute is said to have state. The state of an object is the value of the attributes of an object at any one time. For example, in the example presented in the "Objects" section, the state of the object aPoint is X = 200 and Y = 100 .
Because it is useful to model the state of an
object only when it is necessary to perform some action based on
the state of that object, state is generally not shown on design
models. However, if you need to
In Figure 6.7, the state of the object jimmy is hair = red , height = 6'0'' , and position = prone .
|
|
|