当前位置:网站首页>Class and super, inheritance
Class and super, inheritance
2022-07-19 07:01:00 【I'm Zha Zha Hui】
List of articles
1. Define a class ( There is no limit to which class is defined ):
requirement : a. You need a class variable
b. Need to have >=2 Multiple object variables
c. Define a method : Print class variables and object variables
d. Use print Print object -> Output is This is a object
e. Instantiate two objects : And the sum of two objects equals 2
f. Add a temporary variable to the object temp_var
class Student:
college = ' Xi'an Peihua college '
def __init__(self, name, age):
self.name = name
self.age = age
def print_info(self):
print(Student.college)
print(self.name, self.age)
def __str__(self):
return 'This is a object'
def __add__(self, other):
return self.age + other.age
stu = Student(' Wang Hailin 1', 28)
stu.print_info()
print(stu)
stu1 = Student(' Wang Hailin 2', 30)
print(stu + stu1)
stu.sex = ' male '
print(stu.sex)
2.super Use :
Define a class A, There's another way print_info
Define a class B, There's a way print_info And a way say_something
Define a class C, There's a way say_something
Define a class D, There's a way print_info And a way say_something
Define a class E, Inheritance class A, class B, class C, class D
Instantiate the class E The object of
perform print_info and say_something Method
utilize super, Let's execute print_info Called when the B in print_info
utilize super, Let's execute say_something Called when the C in say_something
utilize super, Let's execute print_info and say_something Called when the
D in print_info and say_something
class A:
def print_info(self):
print(' ahhhh A')
class B:
def print_info(self):
print(' No, no, no, No B')
def say_something(self):
print(' Baba Papa B')
class C:
def say_something(self):
print(' Melancholy long Cen long C')
class D:
def print_info(self):
print(' It's true D')
def say_something(self):
print(' Every bit of D')
class E(C, D, A, B):
def print_info(self):
# utilize super, Let's execute print_info Called when the B in print_info
# super(A, self).print_info()
# utilize super, Let's execute print_info and say_something Called when the D in print_info and say_something
super(C, E).print_info(self)
def say_something(self):
# utilize super, Let's execute say_something Called when the C in say_something
# super().say_something()
super(C, E).say_something(self)
e = E()
e.print_info()
e.say_something()
print(E.__mro__)
3. Define a parent class and a subclass :
requirement : Contains three object variables , And one of the object variables uses _ name
Define a method : After using __ name
Define a subclass to inherit the upper parent class : And define a method with the same name as the parent method (__)
Instantiate subclass objects
Access band _ Object variables for
Access... In the parent class __xxx Method
Access... In the subclass __xxx Method
class Person:
def __init__(self, name, age, sex):
self.name = name
self.age = age
self._sex = sex
def __print_info(self):
print(self.age)
class Student(Person):
def __print_info(self):
print(self.name)
stu = Student(' Zhang San ', 12, ' male ')
print(stu._sex)
stu._Person__print_info()
stu._Student__print_info()
边栏推荐
- Évaluation des performances de la machine virtuelle Tianyi Cloud Hangzhou (VPS)
- notepad++下划线以及大小写字母置换
- 关于文件上传下载问题
- Alibaba cloud Hangzhou arm ECS performance evaluation
- Homework
- F5 GTM (I): DNS parameters
- 字典、元組和列錶的使用及區別,
- slackware 14.2 安装KDE 5 plasma
- 5G时代服务器在这里面起着什么作用?
- Application case of CS brand SD NAND in air quality inspection industry
猜你喜欢

Performance evaluation and comparison of lightweight application servers of major cloud service manufacturers, Alibaba cloud, Tencent cloud, Huawei cloud, and ucloud

Xiaodi network security - Notes (3)

快速掌握sort命令,tr命令
![Minecraft整合包 [GTNH]格雷科技:新视野 服务器搭建教程](/img/59/d5f226f57cfd7d28d5a76ff38fae16.png)
Minecraft整合包 [GTNH]格雷科技:新视野 服务器搭建教程

ARM服务器搭建 我的世界(MC) 1.18.2 版私服教程

文本三剑客之awk命令--截取

小迪网络安全-笔记(5)

Ucloud Shanghai arm cloud server evaluation

小迪网络安全-笔记(3)

解决sonar的单元测试的覆盖率会为0问题
随机推荐
[CS Genesis] comparative analysis of advantages and disadvantages of SD NAND and raw NAND
Redraiment的遭遇
Wireshark packet capture: message information
小迪网络安全-笔记(2)
Solve the problem that the unit test coverage of sonar will be 0
小迪网络安全笔记 信息收集-CDN绕过技术(7)
Xiaodi network security - Notes (3)
Xiaodi - network security notes (1)
数据保护/磁盘列阵RAID保护 IP段103.103.188.xxx
Xiaodi network security - note encryption coding algorithm (6)
高防服务器是如何确认哪些是恶意IP/流量?ip:103.88.32.XXX
[automated testing] - robotframework practice (I) building environment
小迪-网络安全 笔记(1)
函数与随机数
汉诺塔2(函数)
怎么知道网络是否需要用高防服务器?怎么选择机房也是很重要的一点以及后期业务的稳定性
Gnome 安装扩展插件(40.1版本,opensuse tumbleweed)。
103.53.124.X IP段BGP线路和普通的专线有什么区别
Personal information management system
Maomao goes to work (recursive)