当前位置:网站首页>类的定义、类的继承以及super的使用
类的定义、类的继承以及super的使用
2022-08-11 05:35:00 【南屿屿屿】
1.定义一个类(不限定定义哪个类):
要求: a.需要有一个类变量
b.需要有>=2个的对象变量
c.定义一个方法:打印类变量和对象变量
d.使用print打印对象->输出为This is a object
e.实例化两个对象:且两个对象相加等于2
f.为对象添加一个临时变量temp_var
class Person():
name = 'zhang'
def __init__(self, age, sex):
self.age = age
self.sex = sex
def print_a(self):
print(Person.name, self.age, self.sex)
def __str__(self):
return 'This is a object'
def __add__(self, other):
return 2
p1 = Person(21, '男')
p2 = Person(22, '女')
p1.print_a()
print(p1)
print(p1 + p2)
p1.temp_var = '中国'
print(p1.temp_var)
2.super的使用:
定义一个类A, 里面有一个方法print_info
定义一个类B, 里边有一个方法print_info和一个方法say_something
定义一个类C, 里边有一个方法say_something
定义一个类D, 里边有一个方法print_info和一个方法say_something
定义一个类E,继承类A, 类B, 类C,类D
实例化类E的对象
执行print_info和say_something方法
利用super,让执行print_info时调用B中print_info
利用super, 让执行say_something时调用C中say_something
利用super, 让执行print_info和say_something时调用
D中print_info和say_something
class E(A, B, C, D):
def print_info(self):
# super() 将返回一个从 MRO 中 C 之后的类中查找方法的对象。
# super(E, self).print_info()
# 利用super,让执行print_info时调用B中print_info
super(A, self).print_info()
# 利用super, 让执行print_info和say_something时调用D中print_info和say_something
super(C, self).print_info()
def say_something(self):
# super(E, self).say_something()
# 利用super, 让执行say_something时调用C中say_something
super(B, self).say_something()
# 利用super, 让执行print_info和say_something时调用D中print_info和say_something
super(C, self).say_something()
e = E()
e.print_info()
e.say_something()
3.定义一个父类:
要求:包含三个对象变量,且其中一个对象变量使用_命名
定义一个方法:命名使用__命名
定义一个子类继承上边的父类:且定义一个和父类方法名相同的方法(__)
实例化子类的对象
访问带_的对象变量
访问父类中的__xxx方法
访问子类中的__xxx方法
class A:
def __init__(self, a1, a2, _a3):
self.a1 = a1
self.a2 = a2
self._a3 = _a3
def __print_fun(self):
print("This is A fun")
class B(A):
def __print_fun(self):
print("This is B fun")
b = B(1, 2, 3)
print(b._a3)
# 访问父类中的__xxx方法
b._A__print_fun()
# 访问子类中的__xxx方法
b._B__print_fun()
边栏推荐
- iptables 使用脚本来管理规则
- arcgis填坑_3
- SECURITY DAY03 (one-click deployment of zabbix)
- Memory debugging tools Electric Fence
- VMware workstation 16 installation and configuration
- China Mobile Communications Group Co., Ltd.: Business Power of Attorney
- CLUSTER DAY04(块存储应用案例 、 分布式文件系统 、 对象存储)
- 使用路由器DDNS功能+动态公网IP实现外网访问(花生壳)
- 无胁科技-TVD每日漏洞情报-2022-7-18
- The ramdisk practice 1: the root file system integrated into the kernel
猜你喜欢
ETCD集群故障应急恢复-本地数据可用
SSH服务详解
TCP 三次握手、四次断开
AUTOMATION DAY07( Ansible Vault 、 普通用户使用ansible)
ETCD cluster fault emergency recovery - to recover from the snapshot
detectron2,手把手教你训练mask_rcnn
CLUSTER DAY03 (Ceph overview, the deployment of Ceph CLUSTER, Ceph block storage)
项目笔记——随机2
AUTOMATION DAY07 (Ansible Vault, ordinary users use ansible)
mysql数据库安装教程(超级超级详细)
随机推荐
Arcgis小工具_实现重叠分析
SECURITY DAY01 (Monitoring Overview, Zabbix Basics, Zabbix Monitoring Services)
升级到Window11体验
uboot代码解析1:根据目的找主线
What should I do if I forget the user password in MySQL?
SSH服务详解
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-20
ansible批量安装zabbix-agent
VMware workstation 16 installation and configuration
visio文件批量转pdf
slurm集群搭建
文本三剑客——sed 修改、替换
mysql 中登录报错:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)ERROR
Memory debugging tools Electric Fence
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-8-1
China Mobile Communications Group Co., Ltd.: Business Power of Attorney
解决win10安装portal v13/v15要求反复重启问题。
Map Reduce
(1) Software testing theory (0 basic understanding of basic knowledge)
CLUSTER DAY02( Keepalived热备 、 Keepalived+LVS 、 HAProxy服务器 )