Đang chuẩn bị liên kết để tải về tài liệu:
Teach Yourself the C# Language in 21 Days phần 4

Không đóng trình duyệt đến khi xuất hiện nút TẢI XUỐNG

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 7.3 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 console.writeLine 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 7.4 includes the Point structure with a constructor added. Listing 7.4 PointApp2.cs 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 point2.cs- A structure with two data members II. struct Point public int x public int y public Point int x int y this.x x this.y y public Point parameterless constructors not allowed II this.x 0 this.y 0 II class PointApp public static void Main Point pointl new Point Storing More Complex Stuff Structures Enumerators and Arrays 219 Listing 7.4 continued 26 Point point2 new Point 8 8 27 28 point1.x 1 29 point1.y 4 30 31 System.Console.WriteLine Point 1 0 1 32 point1.x point1.y 33 System.Console.WriteLine Point 2 0 1 34 point2.x point2.y 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 7.4 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

Đã 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.