2019年7月4日 类与对象1

d1=类名()  实例化

python2 中 分经典类和新式类

python3 中 只有新式类

属性:

  1.数据属性——变量

  2.函数属性,就是函数,面向对象通常称为方法

  类和对象均用点来方法

class Chinese:
    'chinese people 的类'
    dang='GCD' #定义来属性
    def sui_di_tu_tan():#将自身传递给参数
        print('随地吐痰')
    def cha_dui(self):
        print('插到了前面')


print(Chinese.dang) #调用类
print(Chinese.__dict__) #查看类的属性字典
Chinese.sui_di_tu_tan()
print(Chinese.__dict__['dang'])#通过字典方式寻找属性
Chinese.__dict__['sui_di_tu_tan']()#通过字典方式寻找属性,函数属性+括号
Chinese.cha_dui('xxxx') #如果由self 则必须要传递个对象
p1=Chinese() #加括号 则称为实例化,本质同return

》》》

GCD
{'__module__': '__main__', '__doc__': 'chinese people 的类', 'dang': 'GCD', 'sui_di_tu_tan': <function Chinese.sui_di_tu_tan at 0x100768f28>, 'cha_dui': <function Chinese.cha_dui at 0x100768e18>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>}
随地吐痰
GCD
随地吐痰
插到了前面

扫描二维码关注公众号,回复: 6717823 查看本文章

猜你喜欢

转载自www.cnblogs.com/python1988/p/11129236.html