These two days the new school younger brother study sister only then began to learn the programming language, see the functions and classes when not much understand, recall the time before learning is also the case, so decided to write a most straightforward blog to help you understand what is class (class), what is Object (objects), incidentally mentioned a little function ( function).
A class is an abstract concept that is an abstraction of a class of things. For example, you can think of humans as a class, the common features of this class are: first, standing walking, second, there is a well-developed brain, the above two points are static, describing the objective attributes (attributes). Humans also need to eat, need to sleep, the above two points are dynamic behavior, that is, the method (methods). A class can contain a function, which is a dynamic behavior in a class, that is, a method.
object is the instantiation of the class, the human is a class, and each person is the instantiation of the human, that is, each person is an object, the object has the attributes and methods of the class (everyone is standing to walk, have a well-developed brain, and need to eat and sleep).
It's easy to explain inheritance (inheritance), and inheritance is a small class that inherits large classes. For example, just a human being is a very large class, that student is a relatively small class, the student class can inherit the class of human, after inheriting, the student has the attribute method which the human contains (standing walking, having a developed brain, need to eat to sleep), moreover, the student class because is a smaller class, So there are some characteristics that humans do not have, that is, more refinement, such as students have to go to school this behavior, but humans do not have. In turn, humans are a small class of humans compared to animals, and animals are a big class, and humans can inherit animal species. Humans have some attributes of animals, such as death.
So why is it necessary to create the concept of class in programming? A class is a template that defines properties and methods in a template, and when you create objects (object) of a class, the object automatically has the properties and methods of that class, and it does not need to be manually declared. For example, just humans have standing walking, there is a developed brain two attributes, there are two ways to eat and sleep. So I am creating a specific object of this class, such as Xiaoming, who automatically has the two attributes and two methods. Creating an object may not feel anything, if you create 1000 objects. If there are no classes, create these 1000 objects we need to declare their properties and methods. So the class can improve our programming efficiency.
A class has many other advantages, such as modular programming, that encapsulates a component with a particular functionality into a class. Take a test scene for example, first of all, the exam began to need the teacher to distribute papers, students received the test papers, the need to fill in the name of the study number, and then began to answer the question, the answer to the end, the need to hand in the paper, the teacher received the paper, after the need to correct the examination papers, If the traditional process-oriented programming (that is, no class and object concept, according to the order in which things happen, to achieve a simple test scenario will be more cumbersome), now object-oriented programming (OOP, object oriented programming), First, the object of the scene is abstracted, the two classes of teachers and students are abstracted, and their respective attribute methods are as follows.
Teacher Class:
Method 1: Distribute quiz paper
Method 2: Collect quiz paper
Method 3: Correcting papers
Student class:
Attribute 1: Mood (the mood of the paper after being corrected)
Method 1: Fill in the name of the school number
Method 2: Answer
Method 3: Submit quiz paper
This shows that object-oriented programming will be independent of each module, making the structure more clear, can reduce the production of bugs, improve programming efficiency.