1. Trang chủ
  2. » Công Nghệ Thông Tin

Encapsulation-Important Principle pdf

5 60 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 30,04 KB

Nội dung

The Purpose of Encapsulation Encapsulation is an important principle when defining classes. The idea is that a program that uses a class should not have to worry how that class actually works internally; the program simply creates an instance of a class, and calls the methods of that class. As long as those methods do what they say they will do, the program does not care how they are implemented. For example, when you call the Console.WriteLine method, you don't want to be bothered with all the intricate details of how the Console class physically arranges for data to be output to the screen. A class might need to maintain all sorts of internal state information in order to perform its various methods. This additional state information and activity is hidden from the program that is using the class. Therefore, encapsulation is sometimes referred to as information-hiding. Encapsulation actually has two purposes: 1. To combine methods and data inside a class; in other words, to support classification. 2. To control the accessibility of the methods and data; in other words, to control the use of the class.  Understanding Classification Class is the root word of classification. When you design a class, you systematically arrange information into a meaningful entity. This arranging is an act of classification and is something that everyone does—not just programmers. For example, all cars share common behaviors (they can be steered, stopped, accelerated, and so on) and common attributes (they have a steering wheel, an engine, and so on). People use the word car to mean objects that share these common behaviors and attributes. As long as everyone agrees on what a word means, it all works well; you can express complex but precise ideas in a concise form. Without classification, it's hard to imagine how people could think or communicate at all. Given that classification is so deeply ingrained into the way we think and communicate, it makes sense to try to write programs by classifying the different concepts inherent in a problem and its solution, and then modeling these classes in a programming language. This is exactly what modern object-oriented programming languages, such as Microsoft Visual C#, allow you to do.  Understanding Compound Assignment C# does not allow you to declare any user-defined assignment operators. However, a compound assignment operator (such as +=) is always evaluated in terms of its associated operator (such as +). In other words, this: a += b; Is automatically evaluated as this: a = a + b; In general, the expression a @= b (where @ represents any valid operator) is always evaluated as a = a @ b. If you have declared the appropriate simple operator, it is automatically called when you use its associated compound assignment operator. For example: Hour a = ; int b = ; a += a; // same as a = a + a a += b; // same as a = a + b The first compound assignment expression (a += a) is valid because a is of type Hour, and the Hour type declares a binary operator+ whose parameters are both Hour. Similarly, the second compound assignment expression (a += b) is also valid because a is of type Hour and b is of type int. The Hour type also declares a binary operator+ whose first parameter is an Hour and whose second parameter is an int. Note, however, that you cannot write the expression b += a because that's the same as b = b + a. Although the addition is valid, the assignment is not because there is no way to assign an Hour to the built-in int type. Understanding GUI Events As mentioned earlier, the .NET Framework classes and controls used for building GUIs employ events extensively. You'll see and use GUI events on many occasions in the second half of this book. For example, the Button class derives from the Control class, inheriting a public event called Click of type EventHandler. Let's see this in code. The EventHandler delegate expects two parameters; a reference to the object that caused the event to be raised, and an EventArgs object that contains additional information about the event: namespace System { public delegate void EventHandler(object sender, EventArgs args) ; public class EventArgs { } } namespace System.Windows.Forms { public class Control : { public event EventHandler Click; } public class Button : Control { } } The Button class automatically raises the Click event when you click the button on-screen (how this actually happens is beyond the scope of this book). This arrangement makes it easy to create a delegate for a chosen method and attach that delegate to the required event. The following example shows a Windows form that contains a button called okay, a method called okay_Click, and the code to connect the Click event in the okay button to the okay_Click method: class Example : System.Windows.Forms.Form { private System.Windows.Forms.Button okay; public Example() { this.okay = new System.Windows.Forms.Button(); this.okay.Click += new System.EventHandler(this.okay_Click); } private void okay_Click(object sender, System.EventsArgs args) { // Your code to handle the Click event } } When you use the Designer View in Visual Studio 2005, the IDE generates the code that subscribes methods to events automatically. All you have to do is write the logic in the event handling method. NOTE It is possible to add a method to an event without creating an instance of a delegate. You could replace the following statement: this.okay.Click += new System.EventHandler (this.okay_Click); with this: this.okay.Click += this.okay_Click; However, the Windows Forms designer in Visual Studio 2005 always generates the first version. The events that the GUI classes generate always follow the same pattern. The events are of a delegate type whose signature has a void return type and two arguments. The first argument is always the sender of the event and the second argument is always an EventArgs argument (or a class derived from EventArgs). The sender argument allows you to reuse a single method for multiple events. The delegated method can examine the sender argument and respond accordingly. For example, you can use the same method to subscribe to the Click event fo two buttons (you add the same method to two different events). When the event is raised, the code in the method can examine the sender argument to ascertain which button was clicked. The System.Object Class One of the most important reference types in the .NET Framework is the Object class in the System namespace. To fully appreciate the significance of the System.Object class requires that you understand inheritance, which is described in Chapter 12, “Working with Inheritance.” For the time being, simply accept that all classes are specialized types of System.Object, and that you can use System.Object to create a variable that can refer to any reference type. System.Object is such an important class that C# provides the object keyword as an alias for System.Object. In your code, you can use object or you can write System.Object; they mean exactly the same thing. TIP Use the object keyword in preference to System.Object. It's more direct and it's also consistent with using other keywords that have longer synonyms, as you'll discover in Chapter 9, “Creating Value Types with Enumerations and Structs.” If your program contains a using System; directive, you could also write Object with a capital O (without the System. prefix), but it's best to consistently use the simpler keyword object. In the following example, the variables c and o both refer to the same Circle object. The fact that the type of c is Circle and the type of o is object (the alias for System.Object) in effect provides two different views of the same object: Circle c; c = new Circle(42); object o; o = c; . The Purpose of Encapsulation Encapsulation is an important principle when defining classes. The idea is that a program that uses a class should not have to

Ngày đăng: 01/07/2014, 09:20

w