Knowledge of several nouns:
classes (Class): used to describe a collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.
class variables: class variables are common throughout the instantiated object. Class variables are defined in the class and outside the body of the function. Class variables are not typically used as instance variables.
instance variable: A variable defined in a method that acts only on the class of the current instance.
data members: class variables or instance variables are used to manipulate the data related to the class and its instance objects.
Method: defines a function in a class.
method Overrides: if the method inherited from the parent class does not meet the requirements of the subclass, it can be overridden, which is called the override of the method, also known as the override of the method.
Inheritance: that is, a derived class (derived class) inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is a design where an object of type dog is derived from the animal class, which is the analog "is a (is-a)" Relationship (example, dog is a animal).
instantiation of : Creates an instance of a class, the concrete object of the class.
object: an instance of a data structure defined by a class. The object consists of two data members (class variables and instance variables) and methods.
class definition The syntax format is as follows :
# e:\python36# Coding=gbkclass Classname:script ...
After a class is instantiated, its properties can be used, and in fact, after a class is created, its properties can be accessed through the class name.
The class object supports two operations: Property Reference and instantiation.
For example:
# e:\python36# coding=gbk# class definition class people: #定义基本属性 name = "Age = 1 def F": Return ' Hello World ' # Instantiate class B = people () # Access the properties and methods of the class print ("People class Property Age:", b.age) Print ("People class method F output is:", B.f ())
Execute the above program output as:
people properties of the class Age are: 1
people the method F output of the class is: Hello World
when defining a class, you can define it as a class that can be initialized, so the class may define a name of __init__ () Special method (called the constructor), the instantiation of the class is automatically called __init__ () method.
of course, __init__ () methods can have parameters, parameters are passed __init__ () passed to the instantiation operation of the class.
For example:
# e:\python36# coding=gbk# class people: #定义基本属性 name = ' age = 0 #定义构造方法, used to initialize Def __init__ (self,n,a): Self.name = N Self.age = a If __name__ = = ' __main__ ': #实例化 p = people (' Jack ', ') p.speak ()
the output is: Jack
methods of the class
inside the class, use the def keyword can define a method for a class, unlike a generic function definition, a class method must contain a parameter Self , and for the first parameter :
For example :
# e:\python36# coding=gbk# class people: #定义基本属性 name = ' age = 0 #定义构造方法, used to initialize Def __init__ (self,n,a): Self.name = N Self.age = a def speak (self): print ("My name is%s, this year is%d!") "% (self.name,self.age)) p = people (' Jack ',") P.speak ()
Execute code output result: My name is Jack, 25 years old this year!
Inherited
Here I apply the module, which is a file that contains all of the functions and variables that you define, followed by a. PY name. Modules can be introduced by other programs to use functions such as functions in the module.
Write a Class (student) to inherit the class (people), people the way the application module is introduced, as follows:
# e:\python36# coding=gbk# Introduction module from class_people import people as p# single-inheritance example, inheriting Peopleclass student (p): Grade = ' Def __ Init__ (self,n,a,g): #调用父类的构函 p.__init__ (self,n,a) Self.grade = g #如果父类的方法满足不了需求则重写父类的方法 def SPE AK (self): print ("Not now%d years old, I'm reading the%s grade"% (Self.age,self.grade)) s = student (' Jack ', 26, ' University Three ') S.speak ()
The execution code outputs the result:
My name is Jack, 25 years old this year!
No, I'm 26 years old, and I'm in the third year of college.
Multiple inheritance
Add one more class here
here is a useful point of knowledge: __name__ Property
When a module is introduced for the first time by another program, its main program will run. If we want the module to be introduced, we can use the __name__ property to make the block execute only when the module itself is running.
For example:
Class_people people Pspeaker (p): topic = (, n,a,t): P. (, n,a). Topic = T Speak (): (% (. Name,.age,. TOPIC)) __name__ = =: s = Speaker (,,) S.speak ()
Let's write a multi-inheritance code:
# e:\python36# coding=gbkfrom class_student import student as Tfrom class_speaker import speaker as s# multiple inheritance, inheriting students and orator class Sample (T,s): a = "def __init__ (SELF,N,A,G,T1): t.__init__ (self,n,a,g) s.__init__ (SELF,T1) # overriding the parent class method def speak (self): print ("After the summer vacation is%s, I am still an orator, I am speaking today on the topic of%s"% (self.grade,self.topic)) test = sample ("Jack", 26, ' Senior ', "P Ython ") Test.speak () #方法名同, the default is to call the method of the parent class before the row in parentheses
Execute the code and run the result as:
My name is Jack, 25 years old this year!
No, I'm 26 years old, and I'm in the third year of college.
After the summer vacation is a senior, I am still an orator, the theme of my speech today is Python
As a result, we can see that the methods in class Class_speaker are not running, so each module has a __name__ property, and when its value is ' __main__ ', it indicates that the module itself is running, otherwise it is introduced.
Python basics, Object oriented