Sealed Classes
Số trang: 1
Loại file: pdf
Dung lượng: 16.31 KB
Lượt xem: 1
Lượt tải: 0
Xem trước 1 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Sealed Sử dụng các lớp kế thừa không phải luôn luôn dễ dàng và đòi hỏi phải suy nghĩ. Nếu bạn tạo một giao diện hoặc lớp một trừu tượng, bạn đang cố ý viết cái gì đó sẽ được thừa hưởng từ trong tương lai. Điều rắc rối là dự đoán tương lai là một doanh nghiệp khó khăn.
Nội dung trích xuất từ tài liệu:
Sealed Classes Sealed ClassesUsing inheritance is not always easy and requires forethought. If you create an interfaceor an abstract class, you are knowingly writing something that will be inherited from inthe future. The trouble is that predicting the future is a difficult business. It takes skill,effort, and knowledge of the problem you are trying to solve to craft a flexible, easy-to-use hierarchy of interfaces, abstract classes, and classes. To put it another way, unlessyou consciously design a class with the intention of using it as a base class, its extremelyunlikely that it will function very well as a base class. Fortunately, C# allows you to usethe sealed keyword to prevent a class from being used as a base class if you decide that itshould not be. For example:sealed class LiteralToken : DefaultTokenImpl, IToken{ ...}If any class attempts to use LiteralToken as a base class, a compile-time error will begenerated. A sealed class cannot declare any virtual methods. The sole purpose of thevirtual keyword is to declare that this is the first implementation of a method that youintend to override in a derived class, but a sealed class cannot be derived from.NOTEA struct is implicitly sealed. You can never derive from a struct.Sealed MethodsYou can also use the sealed keyword to declare that an individual method is sealed. Thismeans that a derived class cannot then override the sealed method. You can only seal anoverride method (you declare the method as sealed override).You can think of theinterface, virtual, override, and sealed keywords as follows: • An interface introduces the name of a method. • A virtual method is the first implementation of a method. • An override method is another implementation of a method. • A sealed method is the last implementation of a method.
Nội dung trích xuất từ tài liệu:
Sealed Classes Sealed ClassesUsing inheritance is not always easy and requires forethought. If you create an interfaceor an abstract class, you are knowingly writing something that will be inherited from inthe future. The trouble is that predicting the future is a difficult business. It takes skill,effort, and knowledge of the problem you are trying to solve to craft a flexible, easy-to-use hierarchy of interfaces, abstract classes, and classes. To put it another way, unlessyou consciously design a class with the intention of using it as a base class, its extremelyunlikely that it will function very well as a base class. Fortunately, C# allows you to usethe sealed keyword to prevent a class from being used as a base class if you decide that itshould not be. For example:sealed class LiteralToken : DefaultTokenImpl, IToken{ ...}If any class attempts to use LiteralToken as a base class, a compile-time error will begenerated. A sealed class cannot declare any virtual methods. The sole purpose of thevirtual keyword is to declare that this is the first implementation of a method that youintend to override in a derived class, but a sealed class cannot be derived from.NOTEA struct is implicitly sealed. You can never derive from a struct.Sealed MethodsYou can also use the sealed keyword to declare that an individual method is sealed. Thismeans that a derived class cannot then override the sealed method. You can only seal anoverride method (you declare the method as sealed override).You can think of theinterface, virtual, override, and sealed keywords as follows: • An interface introduces the name of a method. • A virtual method is the first implementation of a method. • An override method is another implementation of a method. • A sealed method is the last implementation of a method.
Gợ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