当前位置:网站首页>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()边栏推荐
猜你喜欢
随机推荐
CLUSTER DAY01(集群及LVS简介 、 LVS-NAT集群 、 LVS-DR集群)
查看CPU和其他硬件温度的软件
华为防火墙-4-安全策略
OA项目之待开会议&历史会议&所有会议
iptables 基础配置
Record a Makefile just written
WiFi Deauth 攻击演示分析 // mdk4 // aireplay-ng// Kali // wireshark //
HCIP 重发布/路由策略实验
Xshell如何连接虚拟机
空间点模式方法_一阶效应和二阶效应
华为防火墙-1-安全区域
SECURITY DAY05(Kali系统 、 扫描与抓包 、 SSH基本防护、服务安全 )
照片的35x45,300dpi怎么弄
【LeetCode】2034. 股票价格波动(思路+题解)双map
【LeetCode】1036. 逃离大迷宫(思路+题解)压缩矩阵+BFS
Map Reduce
HCIP-Spanning Tree (802.1D, Standard Spanning Tree/802.1W: RSTP Rapid Spanning Tree/802.1S: MST Multiple Spanning Tree)
训练分类器
阿里巴巴规范之POJO类中布尔类型的变量都不要加is前缀详解
Top20 bracket matching









