catalogue
- preface
- 1, Create class
- 2, Create object
- __ init__ function
- 3, Object method
- self parameter
- 4, Object property modification
- 5, Inheritance and polymorphism
- 6, Polymorphism
preface
Python is an object-oriented programming language.
Almost everything in Python is an object with properties and methods.
Class is like an object constructor, or a "blueprint" for creating objects.
Inheritance allows us to define a class that inherits all the methods and properties of another class.
A parent class is an inherited class, also known as a base class.
A subclass is a class that inherits from another class, also known as a derived class.
1, Create class
Use the class keyword:
class person: pass class Person(object): pass
In this way, two simple classes are created, in which the object parameter can be used or not.
2, Create object
Now create the object p1 using a class named person:
class Person:#Create person class def __init__(self,name): self.name = name1#Create name attribute p1 = Person("Bill")#Create p1 object print(p1.name)
1.__init__ function
All classes have a name__ init__ (), which is always executed when the class is started.
Use__ init__ () function assigns a value to an object property, or other operations to be performed when creating an object:
#Create a class named person, using__ init__ The function assigns values to name and age class Person: def __init__(self, name, age): self.name = name self.age = age p1 = Person("Bill", 63) print(p1.name) print(p1.age)
Note: each time you create a new object using a class, it will be called automatically__ init__ function
3, Object method
Objects can also contain methods. Methods in an object are functions that belong to that object.
Create a method in the Person class:
class Person: def __init__(self, name, age): self.name = name self.age = age def myfunc(self):#Create method print("Hello my name is " + self.name) p1 = Person("Bill", 63) p1.myfunc()#Call myfunc method
Note: the self parameter is a reference to the current instance of the class and is used to access variables belonging to the class.
1.self parameter
It doesn't have to be named} self. You can call it at will, but it must be the first parameter of any function in the class:
class Person: def __init__(my, name, age): my.name = name my.age = age def myfunc(abc): print("Hello my name is " + abc.name) p1 = Person("Bill", 63) p1.myfunc()
4, Object property modification
For example:
class Person: def __init__(my, name, age): my.name = name my.age = age def myfunc(abc): print("Hello my name is " + abc.name) p1 = Person("Bill", 63) p1.age = 64#The output is 64 print(p1.age) del p1.age#Delete attribute print(p1.age) del p1#Object deletion print(p1)
5, Inheritance and polymorphism
Since it is inheritance, there must be a parent class and a child class. A child class inherits from the parent class. Any class can be a parent class, so the syntax creation is no different.
Create parent class:
Create a file named Person Class containing firstname and lastname Properties and printname method: class Person: def __init__(self, fname, lname): self.firstname = fname self.lastname = lname def printname(self): print(self.firstname, self.lastname) # Use Person to create the object, and then execute the printname method: x = Person("Bill", "Gates") x.printname()
Create subclasses:
To inherit the parent class, send the parent class as a parameter when creating a child class.
class Person: def __init__(self, fname, lname): self.firstname = fname self.lastname = lname def printname(self): print(self.firstname, self.lastname) class Student(Person):#The parent class is a parameter pass x = Student("Elon", "Musk")#If you inherit the parent class, the parameters are the same as the parent class x.printname()
Add__ init__ Functions: adding in subclasses__ init__ () function, the subclass will no longer inherit the parent__ init__ () function.
Sub__ init__ The () function overrides the parent__ init__ () function inheritance.
To keep the parent__ init__ () function inheritance, please add to the parent__ init__ () function call:
class Person: def __init__(self, fname, lname): self.firstname = fname self.lastname = lname def printname(self): print(self.firstname, self.lastname) class Student(Person): def __init__(self, fname, lname): Person.__init__(self, fname, lname)#Pay attention to the parameters here super().__init__(fname,lname)#Using the super function has the same meaning as the uplink function. There is no self in the parameter x = Student("Elon", "Musk") x.printname()
You can also add attributes to subclasses:
class Person: def __init__(self, fname, lname): self.firstname = fname self.lastname = lname def printname(self): print(self.firstname, self.lastname) class Student(Person): def __init__(self, fname, lname, year): super().__init__(fname, lname) self.graduationyear = year#Add a property that the parent class does not have x = Student("Elon", "Musk", 2019)#The parameters should be completed print(x.graduationyear)
In addition to properties, you can add methods.
Polymorphic simple understanding is that the same kind of things have different attributes, for example, cars, cars, buses, etc. they have the same places and different. When people go to drive, they only need to call a certain kind of car. There is no need to write 100 types of cars and call which one they use (too troublesome), Here is a simple topic: using polymorphism to realize the driving method of bus No. 28, 906 and B32 and print it
class Rode: def __init__(self, name): self.name = name def method(self):#Define driving methods print(self.name + "The best way to drive is......") class vehicle: def way(self,name): print("Driving method of this bus:") name.method() #It is to introduce the class called by name, and any name can be used # Rode("rode-28").method() this line has the same meaning as the uplink person = vehicle() rode_28 = Rode("rode-28")#Create an object and pass in the parameters of bus No. 28 person.way(rode_28)#Use the target person to know how to drive the car print("*"*30) rode_906 = Rode("rode-906")#Create an object and pass in the parameters of bus No. 906 person.way(rode_906) print("*"*30) rode_b32 = Rode("rode-b32")#Create an object and pass in the parameters of bus b32 person.way(rode_b32)
6, Object properties and class properties
Class attribute: different from object attribute
class person: name = "person1"#The properties here are actually class properties (called with class names) def __init__(self,name): self.name = name#Object properties, object to call print(person.name)#The output is person1 per = person("Xiao Ming") print(per.name)#The output is Xiao Ming print(person.name)#The output is person1
When the class attribute and the object attribute have the same name, the object attribute has priority over the class attribute. If there is no object attribute, the class attribute is used.
Dynamically add attributes: (only effective for the current object, not for other objects created by the class)
class person: name = "person1"#The properties here are actually class properties (called with class names) def __init__(self,name): self.name = name#Object properties, object to call print(person.name)#The output is person1 per = person("Xiao Ming") print(per.name) per.age = 18 print(per.age) per2 = person("Li Si") print(per2.age)#report errors