当前位置:网站首页>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()
边栏推荐
- 内存调试工具Electric Fence
- HCIP-BGP的选路实验
- 【LeetCode】851.喧闹与富有(思路+题解)
- cloudreve使用体验
- Top20括号匹配
- SECURITY DAY06 ( iptables firewall, filter table control, extended matching, typical application of nat table)
- 训练分类器
- No threat of science and technology - TVD vulnerability information daily - 2022-7-21
- HCIP 重发布/路由策略实验
- 阿里巴巴规范之POJO类中布尔类型的变量都不要加is前缀详解
猜你喜欢
内存调试工具Electric Fence
照片的35x45,300dpi怎么弄
HCIP-BGP的选路实验
VMware workstation 16 installation and configuration
vi display line number in buildroot embedded file system
HCIP OSPF/MGRE综合实验
Solve win10 installed portal v13 / v15 asked repeatedly to restart problem.
OA项目之会议通知(查询&是否参会&反馈详情)
SECURITY DAY06( iptables防火墙 、 filter表控制 、 扩展匹配、nat表典型应用 )
HCIP-生成树(802.1D ,标准生成树/802.1W : RSTP 快速生成树/802.1S : MST 多生成树)
随机推荐
查看可执行文件依赖的库ldd
【LeetCode】2034. 股票价格波动(思路+题解)双map
SECURITY DAY03(一键部署zabbix)
vi display line number in buildroot embedded file system
buildroot设置dhcp
MySQl进阶之索引结构
ETCD集群故障应急恢复-本地数据可用
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-29
CLUSTER DAY03 (Ceph overview, the deployment of Ceph CLUSTER, Ceph block storage)
(1) Software testing theory (0 basic understanding of basic knowledge)
Top20 bracket matching
Xshell如何连接虚拟机
局域网文件传输
华为防火墙-7-dhcp
会议OA项目之我的会议
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-28
阿里巴巴规范之POJO类中布尔类型的变量都不要加is前缀详解
iptables 基础配置
visio文件批量转pdf
TOP2两数相加