tailieunhanh - The Life and Times of an Object

Cuộc đời và lần đầu tiên của một đối tượng, chúng ta hãy recap những gì xảy ra khi bạn tạo và phá hủy một đối tượng. Bạn tạo một đối tượng như thế này: tin nhắn TextBox = new TextBox (); / / TextBox là một loại tài liệu tham khảo Từ quan điểm của bạn | The Life and Times of an Object First let s recap what happens when you create and destroy an object. You create an object like this TextBox message new TextBox TextBox is a reference type From your point of view the new operation is atomic but underneath object creation is really a two-phase process. First the new operation has to allocate some raw memory from the heap. You have no control over this phase of an object s creation. Second the new operation has to convert the raw memory into an object it has to initialize the object. You can control this phase this by using a constructor. NOTE C programmers should note that in C you cannot overload new to control allocation. When you have created an object you can then access its members by using the dot operator. For example People of Earth your attention please You can make other reference variables refer to the same object TextBox ref message How many references can you create to an object As many as you want The runtime has to keep track of all these references. If the variable message disappears by going out of scope other variables such as ref might still exist. Therefore the lifetime of an object cannot be tied to a particular reference variable. An object can only be destroyed when all the references to it have disappeared. NOTE C programmers should note that C does not have a delete operator. The runtime controls when an object is destroyed. Like object creation object destruction is also a two-phase process. The two phases of destruction exactly mirror the two phases of creation. First you have to perform some tidying up. You do this by writing a destructor. Second the raw memory has to be given back to the heap the memory that the object lived in has to be deallocated. Once again you have no control over this phase. The process of destroying an object and returning memory back to the heap is known as garbage collection. Writing Destructors You can use a destructor to perform any tidying up .

crossorigin="anonymous">
Đã 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.