Teach Yourself the C# Language in 21 Days phần 4

cho biết thêm các phương pháp chiều dài tương tự như bạn đã thấy trong danh sách vào những ngày trước. Phương pháp này được khai báo trong dòng 15-21 trong cấu trúc dòng. Như đã được thực hiện trước đó, cấu trúc này sử dụng các thành viên dữ liệu của lớp đường để tính toán chiều dài của dòng. | 218 Day 7 Analysis Listing adds the same length method you have seen in listings on previous days. This method is declared in Lines 15-21 within the Line structure. As was done previously this structure uses the data members of the Line class to calculate the length of the line. This value is placed in the len variable and returned from the method in Line 20 as a double value. The length method is used in the lineApp class. Its value is output using the method in Lines 39-40. Although the Line class has only a single method you could have created a number of methods and properties for the Line structure. You could also have overloaded these methods. Structure Constructors In addition to having regular methods structures can have constructors. Unlike classes if you decide to declare a constructor you must include declarations with parameters. You cannot declare a constructor for a structure that has no parameters. Listing includes the Point structure with a constructor added. Listing A Point Class with a Constructor 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 A structure with two data members II. struct Point public int x public int y public Point int x int y x y public Point parameterless constructors not allowed II 0 0 II class PointApp public static void Main Point pointl new Point Storing More Complex Stuff Structures Enumerators and Arrays 219 Listing continued 26 Point point2 new Point 8 8 27 28 1 29 4 30 31 Point 1 0 1 32 33 Point 2 0 1 34 35 36 Output Point 1 1 4 Point 2 8 8 Analysis A difference between structures and classes is that a structure cannot declare a constructor with no parameters. In Listing you can see that such a constructor has been included in Lines 14-18 however it has been excluded with comments. If you remove the single-line

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã 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.