tailieunhanh - 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? . | 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. .
đang nạp các trang xem trước