TAILIEUCHUNG - Programming C# 4.0 phần 2

Tuy nhiên, chúng ta hoàn toàn có khả năng can thiệp vào quá trình này, thay vì để DLR tự động thực hiện các công việc này, chúng ta sẽ thực hiện việc quyết định những việc gì sẽ làm đối với các truy cập properties hoặc thực thi methods của các object được sử dụng với keyword dynamic. | Defining Classes We can start out with the simplest possible class. It will have no methods and no data so as a model of a plane in our system it leaves something to be desired but it gets us started. If you want to build your own version as you read create a new Console Application project just as we did in Chapter 2. To add a new class use the ProjecwAdd Class menu item or right-click on the project in the Solution Explorer and select Add Class . It ll add a new file for the class and if we call it Visual Studio will create a new source file with the usual using directives and namespace declaration. And most importantly the file will contain a new empty class definition as shown in Example 3-1. Example 3-1. The empty Plane class class Plane Right if we look back at the specification there s clearly a whole bunch of information we ve got about the plane that we need to store somewhere. C gives us a handy mechanism for this called a property. Representing State with Properties Each plane has an identifier which is just a string of letters and numbers. We ve already seen a built-in type ideal for representing this kind of data string. So we can add a property called Identifier of type string as Example 3-2 shows. Example 3-2. Adding a property class Plane string Identifier get set A property definition always states the type of data the property holds string in this case followed by its name. By convention we use PascalCasing for this name see the sidebar on the next page. As with most nontrivial elements of a C program this is followed by a pair of braces and inside these we say that we want to provide a getter and a set-ter for the property. You might be wondering why we need to declare these wouldn t any property need to be gettable and settable But as we ll see these explicit declarations turn out to be useful. 64 Chapter 3 Abstracting Ideas with Classes and Structs PascalCasing and camelCasing Most programming languages including C use whitespace to .

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.