Class and object are two core concepts of object-oriented programming .
1.1 class
Class is a general term for a group of things with the same characteristics or behavior , It is abstract. , Can't be used directly
Class is equivalent to the drawing when making an airplane , It's a template , Is responsible for creating objects , This drawing does not allow the plane to take off , We have to make an aircraft with pilot conditions first .
1.2 object
An object is a concrete existence created by a class , You can use it directly .
Which class creates the object , Just have the properties and methods defined in which class .
In program development , There should be a class first , Another object .
The object is equivalent to an aircraft made of drawings . In a word, a class can be regarded as a drawing , Think of the object as a plane made out of drawings .
Class is a template , Object is created from the template of class , There should be classes before objects .
Class has only one , There can be many objects .
Different objects may have different properties .
What properties and methods are defined in the class , What properties and methods are there in the object , It can't be more , It can't be less .
These are the core concepts in object-oriented programming , One is a class and the other is an object . In a word, the responsibility of a class is very single. It is responsible for creating objects , Define the attributes and methods of the created object in the class . When we have classes, we can use them to create many objects , After the object is created, the method in the class can be called with the object , And set the properties in the class .
We all know that the final result of an object-oriented program is that different objects call different methods . In object-oriented, you want to create objects , There must be a class first , How is that kind designed ? Let's start slowly .
Before using object-oriented development , Needs should be analyzed first , Determine which classes need to be included in the program .
For example, in the following plant war zombie game , To set the sunflower class , Peas, Sagittarius and so on .
In program development , To design a class , There are usually three elements that need to be met :
The name of the great hump :CapWords
Capitalize every word , There is no underline between words
3.1 The determination of class name
Noun extraction , Analyze the nouns appearing in the whole business process , It is usually the class to be found .
3.2 Determination of properties and methods
Description of object characteristics , It can usually be defined as an attribute .
The behavior of objects ( Verb ), It can usually be defined as a method .
Tips : Properties or methods that are not involved in requirements are used in class design , There is no need to consider
practice 1:
demand :
Xiaoming this year 20 year , height 180cm, Every day in the morning run Finishing step , Will go to eat thing
Side dishes this year 18 year , height 165cm, Little dishes don't run , I like small dishes eat thing
answer : Design a human , The attribute contains the name 、 Age 、 height , Behaviors include running and eating , Create two different objects .
practice 2:
demand :
One yellow Of Dogs It's called rhubarb
Meet a stranger bark
See your family Wag the tail
answer : Design a dog class , Attributes are names and dislikes , Behavior is barking and wagging your tail .
more python Please pay attention to python Self study network