继承

1 继承

子没有重写,则继承父

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class A:
x=1
class B(A):
pass
class C(A):
pass
B.x=2
print(A.x,B.x,C.x)
A.x=3
print(A.x,B.x,C.x)




1 2 1
3 2 3

2 super

https://blog.csdn.net/weixin_40734030/article/details/122861895

目的:使得子类初始化的时候调用父类的init

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class test1:
def __init__(self):
self.a=1

class test2(test1):
def __init__(self):
super(test2, self).__init__()
self.b=2

tt=test2()
# print(tt.a)
print(tt.b)
print(tt.a)


2
1
############################
class test1:
def __init__(self):
self.a=1

class test2(test1):
def __init__(self):
# super(test2, self).__init__()
self.b=2

tt=test2()
# print(tt.a)
print(tt.b)
print(tt.a)

2
AttributeError: 'test2' object has no attribute 'a'
1
2
3
4
5

class pointwise_hybird_contrasive(hybird):
def __init__(self,config_roberta, path,num):
super(pointwise_hybird_contrasive, self).__init__(config_roberta, path,num)
super(pointwise_hybird_contrasive, self).\__init\__(config_roberta, path,num)就是对父类hybird的属性进行初始化
Author

Lavine Hu

Posted on

2021-12-09

Updated on

2024-03-24

Licensed under

# Related Post
  1.logging
  2.python内存管理
  3.路径
  4.pandas
  5.
  6.metaclass
  7.unittest
  8.list
Comments

:D 一言句子获取中...