当前位置:网站首页>Class definition, class inheritance, and the use of super
Class definition, class inheritance, and the use of super
2022-08-11 07:09:00 【nanyu yuyu】
1. Define a class (not limited which class is defined):
Requirements: a. Need to have a class variable
b. Need to have >= 2 object variables
c. Define a method: print class variables and object variables
d. Use print to print objects->The output is This is a object
e. Instantiate two objects: and the sum of the two objects is equal to 2
f. Add a temporary variable temp_var to the object
class Person():name = 'zhang'def __init__(self, age, sex):self.age = ageself.sex = sexdef print_a(self):print(Person.name, self.age, self.sex)def __str__(self):return 'This is an object'def __add__(self, other):return 2p1 = Person(21, 'male')p2 = Person(22, 'female')p1.print_a()print(p1)print(p1 + p2)p1.temp_var = 'China'print(p1.temp_var)
Use of 2.super:
Define a class A, which has a method print_info
Define a class B, which has a method print_info and a method say_something
Define a class C, which has a method say_something
Define a class D, There is a method print_info and a method say_something
Define a class E, inherit class A, class B, class C, class D
Instantiate the object of class E
Execute print_info and say_something methods
Use super to call print_info in B when executing print_info
Using super, let say_something in C be called when executing say_something
Using super, let print_info and say_something be called when executing print_info and say_something
print_info and say_something in D
class E(A, B, C, D):def print_info(self):# super() will return an object that looks up methods from classes after C in MRO.# super(E, self).print_info()# Use super to call print_info in B when print_info is executedsuper(A, self).print_info()# Use super to call print_info and say_something in D when print_info and say_something are executedsuper(C, self).print_info()def say_something(self):# super(E, self).say_something()# Using super, let say_something in C be called when say_something is executedsuper(B, self).say_something()# Use super to call print_info and say_something in D when print_info and say_something are executedsuper(C, self).say_something()e = E()e.print_info()e.say_something()
3. Define a parent class:
Requirement: Contains three object variables, and one of the object variables is named with _
Define a method: named with __ named
Define a subclass to inherit the parent class above: and define a and parent classThe method with the same method name (__)
Instantiate the object of the subclass
Access the object variable with _
Access the __xxx method in the parent class
Access the __xxx method in the subclassp>
class A:def __init__(self, a1, a2, _a3):self.a1 = a1self.a2 = a2self._a3 = _a3def __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)# Access the __xxx method in the parent classb._A__print_fun()# Access __xxx methods in subclassesb._B__print_fun()
边栏推荐
- SECURITY DAY03(一键部署zabbix)
- AUTOMATION DAY07 (Ansible Vault, ordinary users use ansible)
- 照片的35x45,300dpi怎么弄
- SECURITY DAY03 (one-click deployment of zabbix)
- buildroot设置dhcp
- iptables 基础配置
- 解决win10安装portal v13/v15要求反复重启问题。
- Slurm基本使用
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-26
- AUTOMATION DAY06( Ansible进阶 、 Ansible Role)
猜你喜欢
ETCD集群故障应急恢复-本地数据可用
AUTOMATION DAY07( Ansible Vault 、 普通用户使用ansible)
智能合约 ——— app评分合约
ETCD cluster fault emergency recovery - to recover from the snapshot
arcgis填坑_2
window7开启远程桌面功能
(1) Software testing theory (0 basic understanding of basic knowledge)
Local yum source build
pytorch下tensorboard可视化深坑
lvm 多盘挂载,合并使用
随机推荐
uboot设置默认的bootdelay
【LeetCode】306.累加数(思路+题解)
查看可执行文件依赖的库ldd
uboot sets the default bootdelay
使用路由器DDNS功能+动态公网IP实现外网访问(花生壳)
树莓派设置静态IP地址
iptables入门
Es常用操作和经典case整理
HCIP WPN实验
Windos10专业版开启远程桌面协助
HCIP MGRE\OSPF综合实验
智能合约 ——— app评分合约
MoreFileRename batch file renaming tool
AUTOMATION DAY07 (Ansible Vault, ordinary users use ansible)
ETCD cluster fault emergency recovery - local data is available
HCIP-生成树(802.1D ,标准生成树/802.1W : RSTP 快速生成树/802.1S : MST 多生成树)
uboot代码解析1:根据目的找主线
SECURITY DAY05(Kali系统 、 扫描与抓包 、 SSH基本防护、服务安全 )
SECURITY DAY02( Zabbix报警机制 、 Zabbix进阶操作 、 监控案例)
阿里巴巴规范之POJO类中布尔类型的变量都不要加is前缀详解