1.1 What Is Object-OrientedProgramDesign?
Object-Oriented programming is a new type of programming paradigm. The main features of this paradigm are:
Program = Object + message
The basic element of object-oriented programs is objects.
The main structural features are:
First, the program generally consists of the definition of the class and the use of the class;
Second, all operations in the program are implemented by sending messages to the object.
1.2 What is an object? What is a class? What is the relationship between objects and classes?
Objects are the data describing their attributes and a set of operations applied to these data are encapsulated together to form a unity.
A class is a set of objects with the same data and operations. That is, a class is a description of an object with the same data structure and operations.
The relationships between classes and objects are abstract and specific relationships. A class is the result of comprehensive abstraction of multiple objects. An object is an instance of a class.
1.3 What are the characteristics of objects in the real world? For example.
Objects in the real world have the following characteristics:
1) each object must have a name to distinguish it from other objects;
2) use attributes to describe certain features of objects;
3) There is a group of operations, each group of Operations determines an object's behavior;
4) the behavior of an object can be divided into two types: one is the behavior acting on itself, and the other is the behavior acting on other objects.
For example, a teacher is an object. Each teacher object has its own name, which is different from other teachers. The instructor has attributes such as number, name, age, title, and major. Instructors can walk, eat, teach, and perform other operations. Walking and eating act on your own behavior, while teaching acts on other objects.
1.4 What is a message? What is the nature of a message?
A request sent by an object to another object becomes a "message ".
A message has the following three properties:
1) the same object can receive multiple messages in different forms and make different responses;
2) messages of the same form can be transmitted to different objects, and the responses can be different;
3) The response to the message is not necessary. The object can respond to the message or not.
1.5 What is abstraction and encapsulation? For example.
Abstraction is a process of summarizing and aggregating the commonalities of relevant things.
For example, it is abstract to classify all students with college students' status into "College Students.
Encapsulation refers to data and implementation operations.CodeFocus on the object and hide the internal details of the object as much as possible.
For example, each washing machine has the factory date, machine number, and other attributes, as well as start, pause, and select operations. When using the washing machine, people only need to press the corresponding button, instead of worrying about the specific internal implementation. This is encapsulation.
1.6 What is inheritance? For example.
Inheritance allows the derived class to use the data and operations of the Base class. At the same time, the derived class can also add new operations and data.
For example, a mammal is an animal with hot blood, hair, and milk to feed the young. A dog is a dog with teeth, meat, a specific bone structure, and a group of mammals. A dog inherits a mammal.
1.7 if there is an inheritance relationship between classes, what are the characteristics of them?
If there is an inheritance relationship between classes, they have the following characteristics:
1) Shared features between classes (including data and code sharing );
2) There are differences or additions between classes (including non-shared data and operation code );
3) the class has a hierarchical structure.
1.8 What is single inheritance and multi-inheritance? For example.
Single inheritance means that each derived class only inherits the features of one base class. For example, a dog inherits from a mammal.
Multi-Inheritance refers to the inheritance relationship between multiple base classes derived from a derived class. For example, a toy car inherits from both toys and cars.
1.9 What is polymorphism? For example.
Polymorphism means that different objects perform different operations when they receive the same message.
For example, there is a window class object and a chess piece class object. When we send a "move" message, the behavior of the two objects is different.
1.10 What are the main advantages of object-oriented programming?
- Improves the reusability of the program;
- Complexity of controllable programs;
- It can improve the maintainability of the program;
- Better support for large-scale program design;
- Enhanced the scope of computer information processing;
It can well adapt to the new hardware environment.