tailieunhanh - What Are Properties?
Tài sản là gì? tài sản là một chéo giữa một lĩnh vực hợp lý và phương pháp vật lý. Bạn sử dụng một tài sản trong cùng một cách chính xác mà bạn sử dụng một lĩnh vực. Hợp lý, tài sản một trông giống như một cánh đồng. | What Are Properties A property is a cross between a logical field and a physical method. You use a property in exactly the same way that you use a field. Logically a property looks like a field. However the compiler automatically translates this field-like syntax into calls to special method-like accessors. A property declaration looks like this AccessModifier Type PropertyName get read accessor code set write accessor code A property can contain two blocks of code starting with the get and set keywords. The get block contains statements that execute when the property is read and the set block contains statements that run when the property is written to. The type of the property specifies the type of data read and written to by the get and set accessors. The next code segment shows the ScreenPosition struct rewritten by using properties. When reading this code notice the following Lowercase x and y are private fields. Uppercase X and Y are public properties. All set accessors are passed the data to be written by using a hidden parameter called value. TIP The fields and properties follow the standard Microsoft Visual C public private naming convention. Public fields and properties should start with an uppercase letter but private fields and properties should start with a lowercase letter. struct ScreenPosition public ScreenPosition int X int Y rangeCheckedX X rangeCheckedY Y public int X get return set rangeCheckedX value public int Y get return set rangeCheckedY value private static int rangeCheckedX int x . private static int rangeCheckedY int y . private int x y In this example a private field directly implements each property. This is only one way to implement a property. All that is required is that a get accessor returns a value of the specified type. Such a value could easily be calculated in which case there would be no need for a physical field. NOTE Although the examples in this chapter show how to define .
đang nạp các trang xem trước