3.1 Abstraction and Program Design
1. Exam Points
Design a class based on a given scenario by identifying the class, attributes and behaviors.
instance variable(non-static) and class variable(static).
Use a class diagram to represent a class.
- It is a good practice to declare instance variables as private, and then add public methods to access them.
Note:
- Nouns for attributes (variables).
- Verbs for behaviors (methods).
- Information of XXX, XXX is normally the class name.
2. Knowledge Points
(1) Abstraction and Program Design
- Abstraction is the process of reducing complexity by focusing on the main idea. (隐藏细节、关注重点的过程, know what, not how)
- Types of abstraction:
- Data abstraction (use attributes of a data type to represent data)
- Procedural abstraction (use methods to represent behaviors)
(2) Data Abstraction
Data abstraction(数据抽象) manages complexity by giving data a name without referencing the specific details of the representation.
- Data can take the form of
a single variable or a collection of data, such as in a class or a set of data.
- Example:

(3) Procedural Abstraction
Procedural abstraction(过程抽象) provides a name for a process and allows a method to be used only knowing what it does, not how it does it.
- Example:

- When designing a class, we use method decomposition(方法分解).
- Through method decomposition (分解), a programmer breaks down larger behaviors of a class into smaller behaviors by creating methods to represent each individual smaller behavior.
- Make a shared feature a method to reuse it, which allows for code reuse, which helps manage complexity.
(4) Design a class
- Prior to implementing a class, it is helpful to take time to
design each class including its attributes and behaviors.
- This design can be represented using
natural language or diagrams.
- Example 1:
use natural language.

- Example 2:
use a class diagram.
A class design diagram for a class will contain three sections.
- Section 1 contains the class name.
- Section 2 contains instance variables and their data types.
- Section 3 contains methods and their return types.
- The + symbol indicates a public designation, and the - symbol indicates a private designation.
3. Exercises