1、继承跟属性赋值super方法调用
class A(object): # 基类
class_a = 20
def __init__(self):
self.a = 10 # 实例对象
def a_nherit(self):
str = '继承的方式'
print("a_nherit%s,"% str)
def a_object(self): # 方法
str = '属性赋值的方式'
print("a_object%s"% str)
def a_super(self): # 方法
str = '父类的基类'
print("a_super%s"% str)
print('*'*10+'方法一'+'*'*10)
# 方法一:继承的方式
class B(A): #
def b_print(self):
print('b_print')
B().a_nherit()
print('*'*10+'方法二'+'*'*10)
# 方法二:属性赋值的方式
class B(object):
object_a = A()
def b_print(self):
print('b_print')
B.object_a.a_object()
print('*'*10+'方法三'+'*'*10)
# 方法三:在基类的基础上,在原来的基础上新增
class C(A):
def a_super(self):
super().a_super()
str = "在父类的基础上新增"
print("add_date%s" % str)
输出结果
**********方法一**********
a_nherit继承的方式
**********方法二**********
a_object属性赋值的方式
**********方法三**********
a_super父类的基类
add_date在父类的基础上新增
2、装饰器的调用
class A():
def __init__(self):
pass
def a_property(self):
print('a_property')
# 不加括号的话是当成是属性来使用
A().a_property
打印结果
结果为空
Process finished with exit code 0
3、类对象调用属性不加括号可以用装饰器(@property)
class A():
def __init__(self):
pass
@property
def a_property(self):
print('a_property')
A().a_property
打印结果
a_property
Process finished with exit code 0