TAILIEUCHUNG - Lecture Introduction to programming with Java - Chapter 7: Object-oriented programming – Additional details

Chapter 7: Object-oriented programming – Additional details. The topics discussed in this chapter are: Object creation - a detailed analysis, assigning a reference, testing objects for equality, passing references as arguments, method-call chaining, overloaded methods, constructors, overloaded constructors. | Chapter 7 Object-Oriented Programming – Additional Details Object Creation - a Detailed Analysis Assigning a Reference Testing Objects For Equality Passing References as Arguments Method-Call Chaining Overloaded Methods Constructors Overloaded Constructors 1 Object Creation - a Detailed Analysis Let's start the chapter with a behind-the-scenes detailed look at what happens when a program instantiates an object and stores its address in a reference variable. Code fragment: 1. Car car; 2. car = new Car(); 3. = 2008; Space is allocated in memory for the car reference variable. The car reference variable will hold the address of an object, but since there's no object created for it yet, it doesn't yet hold a legitimate address. Space is allocated in memory for a new Car object. The address of the allocated space is assigned to car. The car variable's value (the address of a Car object) is used to find the Car object in memory, and then 2008 can be stored in the Car object. Note that for this assignment to work, we're making the simplifying assumption that year is a public instance variable. reference variable declaration object instantiaton Assign 2008 to car's year instance variable 2 Object Creation - a Detailed Analysis Code fragment: 1. Car car; 2. car = new Car(); 3. = 2008; ? 3 Assigning a Reference The result of assigning one reference variable to another is that both reference variables then point to the same object. With both reference variables pointing to the same object, if the object is updated by one of the reference variables, then the other reference variable will notice that change when it attempts to access the object. That can be disconcerting! 4 Assigning a Reference Suppose you want to create two Car objects that are the same except for their color. Your plan is to create the first car, copy the first car to the second car, and then update the second car's color instance variable. Will this code accomplish that? .

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.