TAILIEUCHUNG - Praise for C# 2.0: Practical Guide for Programmers 2005 phần 7

Tham khảo tài liệu 'praise for c# : practical guide for programmers 2005 phần 7', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 138 Chapter 7 Advanced Types Polymorphism and Accessors 30 31 32 33 34 35 36 37 38 39 40 Counter starting at counters c .GetCount for int n 0 n 5 n counters c .GetCount counters c .Tick The methods GetCount Inc and Dec are fully implemented and hence represent common behavior for all subclasses that may derive from Counter. The Tick method on the other hand is abstract requiring subclasses to implement Tick according to their own needs. The two subclasses DownCounter and UpCounter inherit from Counter. When Tick is implemented by either subclass it must be preceded by the modifier override as shown in lines 17 and 22. Hence implementations are specialized for DownCounter and UpCounter. In this case the subclass DownCounter decrements count in its implementation of Tick and the subclass UpCounter increments count in its implementation of Tick. If no modifier precedes an inherited method a warning and an error is generated indicating that the inherited method in the subclass hides the corresponding method of its parent. But if that is the intent then the method Tick must be preceded instead by the modifier new. Using Abstract Classes In the previous class TestAbstractCounter the Main method declares an array of Counter called counters. The array is initialized to one instance each of DownCounter and UpCounter line 27 and hence has a length of two. The instance of DownCounter has an initial value of 9 and the instance of UpCounter has an initial value of 0. For each instance the method Tick is invoked five times lines 32-35 . Depending on whether or not it is an instance of DownCounter or UpCounter the count is either decremented or incremented as shown by the following output Counter starting at 0 01234 Counter starting at 9 98765 Interfaces An interface is a special type of abstract class. It provides the signature but no implementation of all its members. Therefore an interface cannot define data fields .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.