TAILIEUCHUNG - Object Oriented Programming using Java phần 5

lớp học định nghĩa, bạn cần một biến dùng để chỉ đối tượng. Sau đó, tên đầy đủ là hình thức thay đổi tên. Nhưng giả sử bạn đang viết định nghĩa của một phương pháp thể hiện trong lớp học một số. Làm thế nào bạn có thể nhận được một tham chiếu đến các đối tượng có chứa là phương pháp dụ? | class definition you need a variable that refers to the object. Then the full name is of the form . But suppose you are writing the definition of an instance method in some class. How can you get a reference to the object that contains that instance method You might need such a reference for example if you want to use the full name of an instance variable because the simple name of the instance variable is hidden by a local variable or parameter. Java provides a special predefined variable named this that you can use for such purposes. The variable this is used in the source code of an instance method to refer to the object that contains the method. This intent of the name this is to refer to this object the one right here that this very method is in. If x is an instance variable in the same object then this .x can be used as a full name for that variable. If otherMethod is an instance method in the same object then could be used to call that method. Whenever the computer executes an instance method it automatically sets the variable this to refer to the object that contains the method. One common use of thisis in constructors. For example public class Student private String name Name of the student. public Student String name Constructor. Create a student with specified name. name . . More variables and methods. . In the constructor the instance variable called name is hidden by a formal parameter. However the instance variable can still be referred to by its full name . In the assignment statement the value of the formal parameter name is assigned to the instance variable . This is considered to be acceptable style There is no need to dream up cute new names for formal parameters that are just used to initialize instance variables. You can use the same name for the parameter as for the instance variable. There are other uses for this. Sometimes when you are writing an instance method you need .

TỪ KHÓA LIÊN QUAN
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.