tailieunhanh - Lecture Introduction to Java programming - Chapter 10: Thinking in objects

You see the advantages of object-oriented programming from the preceding two chapters. This chapter will demonstrate how to solve problems using the object-oriented paradigm. Before studying these examples, we first introduce several language features for supporting these examples. | Chapter 10 Thinking in Objects Motivations You see the advantages of object-oriented programming from the preceding two chapters. This chapter will demonstrate how to solve problems using the object-oriented paradigm. Before studying these examples, we first introduce several language features for supporting these examples. Objectives To create immutable objects from immutable classes to protect the contents of objects (§). To determine the scope of variables in the context of a class (§). To use the keyword this to refer to the calling object itself (§). To apply class abstraction to develop software (§). To explore the differences between the procedural paradigm and object-oriented paradigm (§). To develop classes for modeling composition relationships (§). To design programs using the object-oriented paradigm (§§). To design classes that follow the class-design guidelines (§). Immutable Objects and Classes If the contents of an object cannot be changed once the object is created, the object is called an immutable object and its class is called an immutable class. If you delete the set method in the Circle class in the preceding example, the class would be immutable because radius is private and cannot be changed without a set method. A class with all private data fields and without mutators is not necessarily immutable. For example, the following class Student has all private data fields and no mutators, but it is mutable. Example public class Student { private int id; private BirthDate birthDate; public Student(int ssn, int year, int month, int day) { id = ssn; birthDate = new BirthDate(year, month, day); } public int getId() { return id; } public BirthDate getBirthDate() { return birthDate; } } public class BirthDate { private int year; private int month; private int day; public BirthDate(int newYear, int newMonth, int newDay) { year = newYear; month = newMonth; day = . | Chapter 10 Thinking in Objects Motivations You see the advantages of object-oriented programming from the preceding two chapters. This chapter will demonstrate how to solve problems using the object-oriented paradigm. Before studying these examples, we first introduce several language features for supporting these examples. Objectives To create immutable objects from immutable classes to protect the contents of objects (§). To determine the scope of variables in the context of a class (§). To use the keyword this to refer to the calling object itself (§). To apply class abstraction to develop software (§). To explore the differences between the procedural paradigm and object-oriented paradigm (§). To develop classes for modeling composition relationships (§). To design programs using the object-oriented paradigm (§§). To design classes that follow the class-design guidelines (§). Immutable Objects and Classes If the contents of an