The Problem with Objects
Số trang: 2
Loại file: pdf
Dung lượng: 9.81 KB
Lượt xem: 1
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Các vấn đề với đối tượng Để hiểu Generics, nó là giá trị tìm kiếm cụ thể tại những vấn đề chúng được thiết kế để giải quyết, đặc biệt khi sử dụng các loại đối tượng. Bạn có thể sử dụng các loại đối tượng như là một tham chiếu đến bất kỳ loại giá trị hoặc biến.
Nội dung trích xuất từ tài liệu:
The Problem with Objects The Problem with ObjectsIn order to understand Generics, it is worth looking in detail at the problems they aredesigned to solve, specifically when using the object type.You can use the object type as a reference to any type of value or variable. All referencetypes automatically inherit (either directly or indirectly) from the System.Object class inthe .NET Framework. You can use this information to create highly generalized classesand methods. For example, many of the classes in the System.Collections namespaceexploit this fact to allow you to create collections of any type. You will also notice in theSystem.Collections.Queue class that you can create queues containing almost anything(you have already been introduced to the collection classes in Chapter 10, “Using Arraysand Collections”). The following fragment shows how to create and manipulate a queueof Circle objects:using System.Collections;...Queue myQueue = new Queue();Circle myCircle = new Circle();myQueue.Enqueue(myCircle);...myCircle = (Circle)myQueue.Dequeue();The Enqueue method adds an object to the head of a queue, and the Dequeue methodremoves the object at the other end of the queue. These methods are defined like this:public void Enqueue( object item );public object Dequeue();Because the Enqueue and Dequeue methods manipulate objects, you can operate onqueues of Circles, PhoneBooks, Clocks, or any of the other classes you have seen inearlier exercises in this book. However, it is important to notice that you have to cast thevalue returned by the Dequeue method to the appropriate type because the compiler willnot perform the conversion from the object type automatically. If you dont cast thereturned value, you will get the compiler error “Cannot implicitly convert type object toCircle” as shown in the following code fragment:Circle myCircle = new Circle();myQueue.Enqueue(myCircle);...myCircle = (Circle)myQueue.Dequeue(); // Cast is mandatoryThis need to perform an explicit cast denigrates much of the flexibility afforded by theobject type. It is very easy to write code such as this:Queue myQueue = new Queue();Circle myCircle = new Circle();myQueue.Enqueue(myCircle);...Clock myClock = (Clock)myQueue.Dequeue();Although this code will compile, it is not valid and throws aSystem.InvalidCastException at runtime. The error is caused by trying to store areference to a Circle in a Clock variable, and the two types are not compatible. This erroris not spotted until runtime because the compiler does not have enough information. Itcan only determine the real type of the object being dequeued at runtime.Another disadvantage of using the object approach to create generalized classes andmethods is that it can use additional memory and processor time if the runtime needs toconvert an object into a value type and back again. Consider the following piece of codethat manipulates a queue of ints:Queue myQueue = new Queue();int myInt = 99;myQueue.Enqueue(myInt); // box the int to an object...myInt = (int)myQueue.Dequeue(); // unbox the object to an intThe Queue data type expects the items it holds to be reference types. Enqueueing a valuetype, such as an int, requires that it is boxed to convert it into a reference type. Similarly,dequeueing into an int requires that the item is unboxed to convert it back to a value type.See the sections “Boxing” and “Unboxing” in Chapter 8 for more details. Althoughboxing and unboxing happen transparently, they add a performance overhead as theyinvolve dynamic memory allocations. While this overhead is small for each item, it addsup when a program creates queues of large numbers of value types.
Nội dung trích xuất từ tài liệu:
The Problem with Objects The Problem with ObjectsIn order to understand Generics, it is worth looking in detail at the problems they aredesigned to solve, specifically when using the object type.You can use the object type as a reference to any type of value or variable. All referencetypes automatically inherit (either directly or indirectly) from the System.Object class inthe .NET Framework. You can use this information to create highly generalized classesand methods. For example, many of the classes in the System.Collections namespaceexploit this fact to allow you to create collections of any type. You will also notice in theSystem.Collections.Queue class that you can create queues containing almost anything(you have already been introduced to the collection classes in Chapter 10, “Using Arraysand Collections”). The following fragment shows how to create and manipulate a queueof Circle objects:using System.Collections;...Queue myQueue = new Queue();Circle myCircle = new Circle();myQueue.Enqueue(myCircle);...myCircle = (Circle)myQueue.Dequeue();The Enqueue method adds an object to the head of a queue, and the Dequeue methodremoves the object at the other end of the queue. These methods are defined like this:public void Enqueue( object item );public object Dequeue();Because the Enqueue and Dequeue methods manipulate objects, you can operate onqueues of Circles, PhoneBooks, Clocks, or any of the other classes you have seen inearlier exercises in this book. However, it is important to notice that you have to cast thevalue returned by the Dequeue method to the appropriate type because the compiler willnot perform the conversion from the object type automatically. If you dont cast thereturned value, you will get the compiler error “Cannot implicitly convert type object toCircle” as shown in the following code fragment:Circle myCircle = new Circle();myQueue.Enqueue(myCircle);...myCircle = (Circle)myQueue.Dequeue(); // Cast is mandatoryThis need to perform an explicit cast denigrates much of the flexibility afforded by theobject type. It is very easy to write code such as this:Queue myQueue = new Queue();Circle myCircle = new Circle();myQueue.Enqueue(myCircle);...Clock myClock = (Clock)myQueue.Dequeue();Although this code will compile, it is not valid and throws aSystem.InvalidCastException at runtime. The error is caused by trying to store areference to a Circle in a Clock variable, and the two types are not compatible. This erroris not spotted until runtime because the compiler does not have enough information. Itcan only determine the real type of the object being dequeued at runtime.Another disadvantage of using the object approach to create generalized classes andmethods is that it can use additional memory and processor time if the runtime needs toconvert an object into a value type and back again. Consider the following piece of codethat manipulates a queue of ints:Queue myQueue = new Queue();int myInt = 99;myQueue.Enqueue(myInt); // box the int to an object...myInt = (int)myQueue.Dequeue(); // unbox the object to an intThe Queue data type expects the items it holds to be reference types. Enqueueing a valuetype, such as an int, requires that it is boxed to convert it into a reference type. Similarly,dequeueing into an int requires that the item is unboxed to convert it back to a value type.See the sections “Boxing” and “Unboxing” in Chapter 8 for more details. Althoughboxing and unboxing happen transparently, they add a performance overhead as theyinvolve dynamic memory allocations. While this overhead is small for each item, it addsup when a program creates queues of large numbers of value types.
Tìm kiếm theo từ khóa liên quan:
ngôn ngữ lập trình lập trình ngôn ngữ C# C# Summarizing Keyword Combinations The Problem with ObjectsGợi ý tài liệu liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 271 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 261 0 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 261 0 0 -
Giáo trình Lập trình cơ bản với C++: Phần 1
77 trang 230 0 0 -
Bài giảng Một số hướng nghiên cứu và ứng dụng - Lê Thanh Hương
13 trang 221 0 0 -
Giáo án Tin học lớp 11 (Trọn bộ cả năm)
125 trang 214 1 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 202 0 0 -
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 177 0 0 -
Giáo trình Lập trình C căn bản: Phần 1
64 trang 169 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 162 0 0