TAILIEUCHUNG - Temperless Comparing Fields

Comparing Fields and Methods First, let's recap the original motivation for using methods to hide fields. Consider the following struct that represents a position on a screen as an (X, Y) coordinate pair: struct ScreenPosition { public ScreenPosition(int x, int y) { = rangeCheckedX(x); = rangeCheckedY(y); } public int X; public int Y; private static int rangeCheckedX(int x) { if (x 1280 | Comparing Fields and Methods First let s recap the original motivation for using methods to hide fields. Consider the following struct that represents a position on a screen as an X Y coordinate pair struct ScreenPosition public ScreenPosition int x int y rangeCheckedX x rangeCheckedY y public int X public int Y private static int rangeCheckedX int x if x 0 x 1280 throw new ArgumentOutOfRangeException X return x private static int rangeCheckedY int y if y 0 y 1024 throw new ArgumentOutOfRangeException Y return y The problem with this struct is that it does not follow the golden rule of encapsulation it does not keep its data private. Public data is a bad idea because its use cannot be checked and controlled. For example the ScreenPosition constructor range checks its parameters but no such check can be done on the raw access to the public fields. Sooner or later probably sooner either X or Y will stray out of its range possibly as the result of a programming error ScreenPosition origin new ScreenPosition 0 0 . int xpos -100 Oops The common way to solve this problem is to make the fields private and add an accessor method and a modifier method to respectively read and write the value of each private field. The modifier methods can then range-check the new field values because the constructor already checks the initial field values. For example here s an accessor GetX and a modifier SetX for the X field. Notice how SetX checks its parameter value struct ScreenPosition public int GetX return public void SetX int newX rangeCheckedX newX . private static int rangeCheckedX int x . private static int rangeCheckedY int y . private int x y The code now successfully enforces the range constraints which is good. However there is a price to pay for this valuable guarantee ScreenPosition no longer has a natural fieldlike syntax it uses awkward method-based syntax instead. The following example increases the value of X by 10. To do .

TÀI LIỆU LIÊN QUAN
10    127    1
6    150    1
7    127    1
5    125    1
6    127    1
6    115    1
6    122    1
6    174    1
7    122    1
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.