click below
click below
Normal Size Small Size show me how
Chapter 10
Classes and Objects: A Deeper Look
Term | Definition |
---|---|
Composition | A capability that allows a class to have references to objects of other classes as members. The concept that a class can have references to objects of other classes as members. Sometimes called a has-a relationship |
Overloaded Constructors | Enable objects of a class to be conveniently initialized in different ways. To overload constructors, simply provide multiple constructor declarations with different signatures |
1. Constructor Initializer | Following the constructor header with the constructor initializer : this (args) invokes the matching overloaded constructor in the same class. Ex: public Time2(Time2 time)... : this(time.Hour, time.Minute, time.second){} |
2. Constructor Initializer. | Constructor initializers are a popular way to reuse initialization code provided by one of the class's constructors rather than defining similar code in another constructor's body. Every object can access a reference to itself with keyword this |
Using Properties | When implementing a method of a class, using the class's properties to access the class's private data simplifies code maintenance and reduces the likelihood of errors |
Resource Leak | When all the references to the object that manages the resource are lost before the resource is explicitly released. When this happens, the app can no longer access the resource to release it |
Garbage Collection | The Common Language Runtime(CLR) performs automatic memory management by using a garbage collector to reclaim the memory occupied by objects that are no long in use, so the memory can be used for other objects |
1. Destructors | When there are no more references to an object, the object becomes eligible for destruction. |
2. Destructors | Every object has a special member, called a destructor, that's invoked by garbage collector to perform termination housekeeping on an object before the garbage collector reclaims the object's memory |
Declaring a Constructor | A destructor is declared like a parameterless constructor, except that its name is the class name, preceded by a tilde(~), and it has no access modifier in its header. |
Eligible for Garbage Collection | After the garbage collector calls the object's destructor, the object becomes eligible for garbage collection. The memory for such an object can be reclaimed by the garbage collector |
static Variable | Used in such cases where only one copy of a particular variable should be shared by all objects of a class. A static variable represents classwide information. The declaration of a static variable begins with the keyword static |
Classwide Information | When all objects of the class share the same piece of data. |
Immutable | Cannot be modified after they're created. string objects in C# are immutable |
Principle of Least Privilege | States that code should be granted the amount of privilege and access needed to accomplish its designated task, but no more. This principle is fundamental to good software engineering |
1. readonly | C# provides keyword readonly to specify that an instance variable of an object is not modifiable and that any attempt to modify it after the object is constructed is an error. |
2. readonly | Ex: private readonly int INCREMENT;... declares readonly instance variable INCREMENT of type int. Readonly variables are declared with all capital letters by convention. Readonly variables should be initialized by each of the class's constructors |
3. readonly | Declaring an instance variable as readonly helps enforce the principle of least privilege. If an instance varible should not be modified after the object is constructed, declare it to be readonly to prevent modification |
Information Hiding | When a class hides the details of their implementation from their clients |
Data Abstraction | The concept that the client cares about what functionality a class offers, not about how that functionality is implemented. |
Abstract Data Types(ADTs) | Captures two notions: a data representation and the operations that can be performed on that data |
Queue Abstract Data Type | Another ADT is a queue, which is similar to a "waiting line". Clients place items in a queue one at a time via an enqueue operation, then retrieve them one at a time via a dequeue operation. A queue returns items in first-int, first-out(FIFO) order |
First-In, First-Out(FIFO) Order | The first item inserted in a queue is the first removed |
Visual Studio Class View | Displays the fields, methods, and properties for all classes in a project |
Visual Studio Object Browser | Lists all classes in the .NET library. You can use the Object Browser to learn about the functionality provided by a specific class |
1. Object Initiallzers | Allow you to create an object and initialize its public properties(and public instance variables, if any) in the same statement. |
2. Object Initiallzers | Can be useful when class doesn't provide appropriate constructor to meet your needs, but does provide properties that you can use to manipulate the class's data. Ex: Time2 atime = new Time2 {Hour = 14, Minute = 30, Second = 12}; |
ToString Method | An object's ToString method can be called implicitly when an object appears in code where a string is needed |
Public Services, Public Interfaces | The public members of a class |