tailieunhanh - Thinking in C# phần 4

Lớp này không làm bất cứ điều gì, nhưng bạn có thể tạo ra một đối tượng: DataOnly d = mới DataOnly (); Cả hai classname và các lĩnh vực ngoại trừ s trước công chúng từ. Điều này có nghĩa rằng họ có thể nhìn thấy tất cả các đối tượng khác. Bạn có thể gán giá trị cho các thành viên dữ liệu có thể nhìn thấy, nhưng trước tiên bạn phải biết làm thế nào để tham khảo cho một thành viên của một đối tượng. Điều này được thực hiện bằng cách nêu rõ tên của. | But be careful with your assumptions. In general it s difficult to anticipate how a class can be reused especially a general-purpose class. Unless you declare a method as virtual you prevent the possibility of reusing your class through inheritance in some other programmer s project simply because you couldn t imagine it being used that way. Initialization and class loading In more traditional languages programs are loaded all at once as part of the startup process. This is followed by initialization and then the program begins. The process of initialization in these languages must be carefully controlled so that the order of initialization of statics doesn t cause trouble. C for example has problems if one static expects another static to be valid before the second one has been initialized. C doesn t have this problem because it takes a different approach to loading. Because everything in C is an object many activities become easier and this is one of them. As you will learn more fully in the next chapter the compiled code for a set of related classes exists in their own separate file called an assembly. That file isn t loaded until the code is needed. In general you can say that Class code is loaded at the point of first use. This is often not until the first object of that class is constructed but loading also occurs when a static field or static method is accessed. The point of first use is also where the static initialization takes place. All the static objects and the static code block will be initialized in textual order that is the order that you write them down in the class definition at the point of loading. The statics of course are initialized only once. Initialization with inheritance It s helpful to look at the whole initialization process including inheritance to get a full picture of what happens. Consider the following code c07 The full process of initialization. using System class Insect int i 9 internal int j Chapter 7 Reusing Classes .

TỪ KHÓA LIÊN QUAN