当前位置:网站首页>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()边栏推荐
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-29
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-8-3
- iptables 基础配置
- OA项目之项目简介&会议发布
- detectron2,手把手教你训练mask_rcnn
- window10吐槽
- SECURITY DAY01(监控概述 、 Zabbix基础 、 Zabbix监控服 )
- CLUSTER DAY02( Keepalived热备 、 Keepalived+LVS 、 HAProxy服务器 )
- Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-18
- 查看内核版本和发行版版本
猜你喜欢

MoreFileRename批量文件改名工具

Top20 bracket matching

buildroot setup dhcp

Basic use of Slurm

HCIP-Spanning Tree (802.1D, Standard Spanning Tree/802.1W: RSTP Rapid Spanning Tree/802.1S: MST Multiple Spanning Tree)

HCIP-BGP的选路实验

OA项目之我的会议(会议排座&送审)

cloudreve使用体验

SECURITY DAY04( Prometheus服务器 、 Prometheus被监控端 、 Grafana 、 监控数据库)

核方法 Kernel method
随机推荐
MoreFileRename batch file renaming tool
(1) Software testing theory (0 basic understanding of basic knowledge)
FusionCompute8.0.0实验(1)CNA及VRM安装
空间点模式方法_一阶效应和二阶效应
Basic use of Slurm
Threatless Technology-TVD Daily Vulnerability Intelligence-2022-7-28
【LeetCode】2034. 股票价格波动(思路+题解)双map
SECURITY DAY01 (Monitoring Overview, Zabbix Basics, Zabbix Monitoring Services)
Top20 bracket matching
训练分类器
查看内核版本和发行版版本
OA项目之待开会议&历史会议&所有会议
windows10安全中心显示“修正未完成”
CLUSTER DAY03 (Ceph overview, the deployment of Ceph CLUSTER, Ceph block storage)
HCIP OSPF动态路由协议
arcgis填坑_4
CLUSTER DAY02( Keepalived热备 、 Keepalived+LVS 、 HAProxy服务器 )
arcgis填坑_1
Record a Makefile just written
AUTOMATION DAY06 (Ansible Advanced, Ansible Role)