当前位置:网站首页>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()
边栏推荐
- 八股文之jvm
- CLUSTER DAY02( Keepalived热备 、 Keepalived+LVS 、 HAProxy服务器 )
- ETCD cluster fault emergency recovery - local data is available
- China Mobile Communications Group Co., Ltd.: Business Power of Attorney
- Memory debugging tools Electric Fence
- (1) Software testing theory (0 basic understanding of basic knowledge)
- View the library ldd that the executable depends on
- 阿里巴巴规范之POJO类中布尔类型的变量都不要加is前缀详解
- ETCD Single-Node Fault Emergency Recovery
- 命令输出给变量
猜你喜欢
随机推荐
Arcgis小工具_实现重叠分析
Xshell如何连接虚拟机
HPC平台搭建
slurm集群搭建
uboot设置默认的bootdelay
arcgis填坑_3
ovnif摄像头修改ip
HCIP BGP built adjacent experiment
(2) Software Testing Theory (*Key Use Case Method Writing)
AUTOMATION DAY07 (Ansible Vault, ordinary users use ansible)
华为防火墙-1-安全区域
【LeetCode】2034. 股票价格波动(思路+题解)双map
window10吐槽
核方法 Kernel method
【LeetCode】1036. 逃离大迷宫(思路+题解)压缩矩阵+BFS
arcgis填坑_2
Eight-legged text jvm
局域网文件传输
八股文之redis
HCIP WPN实验