Python class creation and relationships
1. Define and create classes
To create a class, the keyword "class" is used.
Example: Create a class named myname as follows
class myname:
x =100
print(myname)
Explanation: The class name is myname, and x is its property name.
1. Private property
Some properties of the class cannot be called by the object, and can only be used inside the class.
Example:
class jifen:
_count=0
_name=''
def __init__(self,name,count):
self._count += count
self._name =name
def seek (self):
print(self._name+'积分'+str(self._count))
m=jifen('小王',300)
m.seek()
output:
小王积分300
Explanation: Use + to connect strings when outputting, because count is a number, so use the str function to convert it into a string.
2. Private method
The same is true for private methods and private properties, which can be compared to memory and understanding.
class jifen:
def _jisuan(self,count):
print('更改积分为:'+str(count))
m=jifen
m._jisaun(200)
output:
Traceback (most recent call last):
File "C:\Users\86139\学习\main.py", line 420, in <module>
m._jisaun(200)
AttributeError: type object 'jifen' has no attribute '_jisaun'
Explanation: The function jisuan is a private method, and the call reports an error.
2. Create objects
Then the above code continues to improve, using the class of myname to create an object.
Example:
print("Flase")
class myname:
x = 100
user = myname()
print(myname.x)
output:
100
Explanation: Create the user object in this class and output the value of x.
Three, init () function
Definition: All classes have a function named init, which is executed when the class is initialized. Use the init function to assign values to object properties
, or perform other operations while creating the object.
Example:
class myname:
def __init__(self,name,age):
self.name= name
self.age= age
user = myname("小王",20)
print(user.name)
print(user.age)
output:
小王
20
Explanation: Use the init function to assign values to name and age.
4. Object method
Personal understanding: the init function is responsible for initializing and assigning properties; the object method is to define another function call
Attribute execution output.
Example:
class myname:
def __init__(self,name,age):
self.name= name
self.age= age
def my(self):
print("我的名字:"+self.name,"年龄:"+self.age)
user = myname("小王","二十")
user.my()
output:
我的名字:小王 年龄:二十
Explanation: Assign the properties of the object through the init function, and then call the output through the my function.
5. pass statement
The principle of the pass statement in the previous loop is the same, and it can be compared to remember and understand.
class myname:
pass
Explanation: The definition of class cannot be empty, if you need a class without content for some kind of search. You need to put the pass statement to avoid error reporting.
5. Change of object properties
1. replace
Replace: Literally change the assignment of the attribute.
class myname:
def __init__(self,name,age):
self.name= name
self.age= age
def my(self):
print("我的名字:"+self.name,"年龄:"+self.age)
user = myname("小王","二十")
user.age = 22
print(user.age)
output:
22
Explanation: Change the assignment of the age attribute from 20 to 22.
2. delete
Delete: similarly delete the attribute directly from the class.
Example:
class myname:
def __init__(self,name,age):
self.name= name
self.age= age
del user.age
def my(self):
print("我的名字:"+self.name,"年龄:"+self.age)
user = myname("小王","二十")
del user.age
print(user.age)
Output: an error will be reported
Explanation: age has been deleted and does not have this attribute.
Six, the relationship between classes
1. Inheritance
(1) Single inheritance
The inherited class is called: base class, parent class, super class; the inheritor is called: subclass, and a subclass can inherit any properties and methods of its parent class.
Class inheritance syntax:
class 类名 (被继承父类的名字)
.......
Example:
class father:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class sun(father):
pass
x = sun("小王", "憨憨")
x.printname()
output:
小王 憨憨
Logical explanation: In the above code, a parent class father and a subclass sun are defined; the content of the sun class is empty for pass. Pass: class sun(father): inherits the attribute method of the father class.
(2) Multiple inheritance
A subclass can inherit two or more parent classes at the same time, this situation is called: multiple inheritance.
Example:
class animals:
color =''
weight=0
def jump(self):
print('我能跳')
class cats:
def maomi(self):
print('喵喵')
class mycat (animals,cats):
def catch(self):
print('我才能抓到老鼠')
c=mycat()
c.jump()
c.maomi()
c.catch()
output:
我能跳
喵喵
我才能抓到老鼠
2. Add init function in inheritance
Then change the above code.
Correct example:
class father:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class sun(father):
def __init__(self, fname, lname):
father.__init__(self, fname, lname)
x = sun("小王", "憨憨")
x.printname()
output:
小王 憨憨
Error example:
class father:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class sun(father):
def __init__(self, fname, lname):
x = sun("小王", "憨憨")
x.printname()
output:
File "C:\Users\86139\学习\main.py", line 388
x = sun("小王", "憨憨")
IndentationError: expected an indented block
Explanation: The reason for the error is that the subclass has not called the init of the parent class, and the init function still needs to be called in order to maintain the inheritance of the parent class.
3. super function
The role of the super function: allows subclasses to inherit all the properties and methods of their parent class.
class father:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class sun(father):
def __init__(self, fname, lname):
super().__init__( fname, lname)
x = sun("小王", "憨憨")
x.printname()
output:
小王 憨憨
4. Add attributes
Function: Add an attribute to the subclass.
Easy to understand with simple changes to the above code.
Example:
class father:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class sun(father):
def __init__(self, fname, lname):
super().__init__( fname, lname)
self.xinxi =22
x = sun("小王", "憨憨")
x.printname()
print(x.xinxi)
output:
小王 憨憨
22
understand
5. Add method
Role: Add a method to the subclass.
Easy to understand with simple changes to the above code.
Example:
class father:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class sun(father):
def __init__(self, fname, lname,xinxi):
super().__init__( fname, lname)
self.xinxi =22
def san(self):
print("姓名",self.firstname,"性别",self.lastname,"年龄", self.xinxi)
x = sun("小王", "憨憨","22")
x.san()
output:
姓名 小王 性别 憨憨 年龄 22