zoukankan      html  css  js  c++  java
  • python的面向对象-类的数据属性和实例的数据属性相结合-无命名看你懵逼不懵逼系列

    1、

    class Chinese:
        country='China'
        def __init__(self,name):
            self.name=name
        def play_ball(self,ball):
            print('%s 正在打 %s' %(self.name,ball))
    p1=Chinese("北爷")
    print(p1.country)#这是访问的类的数据属性
    p1.country="日本人"#增加了一个实例的数据属性
    print(Chinese.country)#调用类的数据属性
    print(p1.country)#调用实例的数据属性,因为上面增加了实例的country属性是日本人
    
    C:python35python3.exe D:/pyproject/day24/类属性与实例属性结合.py
    
    China
    
    China
    
    日本人

    2、

    class Chinese:
        country = '中国'
        def __init__(self,name):
            print("实例化先运行init------->")
            self.name=name
        def play_ball(self,ball):
            print('%s 正在打 %s' %(self.name,ball))
    def shi_li_hua():#定义一个实例化函数
        name=input(">>>")#接受一个值赋值给name
        p1=Chinese(name)#实例化一个p1实例
        print(p1.country)#调用实例的类的数据属性
    shi_li_hua()#运行shilihua这个函数,前面都是定义,把那些加载到内存,这才是程序运行的第一步,然后风湿理论向上找,作用域

    3、

    country="中国"
    class Chinese:
        country = '中国+++'
        def __init__(self,name):
            self.name=name
            print("---->",country)#这个country不是用.调用的,既不是类的属性也不是实例的属性,就是一个普通的变量,遵循风湿理论
        def play_ball(self,ball):
            print('%s 正在打 %s' %(self.name,ball))
    p1=Chinese("北爷")#实例化一个p1实例
    
    C:python35python3.exe D:/pyproject/day24/类属性与实例属性结合.py
    
    ----> 中国

    4、

    country="中国++"
    class Chinese:
        country = '中国'
        def __init__(self,name):
            self.name=name
            print("普通变量",country)#这个country不是用.调用的,既不是类的属性也不是实例的属性,就是一个普通的变量
        def play_ball(self,ball):
            print('%s 正在打 %s' %(self.name,ball))
    print(country)#调用全局作用域的country
    print(Chinese.country)#调用类的数据属性country
    p1=Chinese("北爷")#实例化一个p1实例
    print(p1.country)#调用实例的类的数据属性,实例字典里面没有,就去类字典里去找
    
    C:python35python3.exe D:/pyproject/day24/类属性与实例属性结合.py
    
    中国++
    
    中国
    
    普通变量 中国++
    
    中国

    5、

    class Chinese:
        country='China'
        l=["a","b"]#存在类的属性字典里面
        def __init__(self,name):
            self.name=name
        def play_ball(self,ball):
            print('%s 正在打 %s' %(self.name,ball))
    p1=Chinese("北爷")
    print(p1.l)#实例p1调用类的数据属性l
    p1.l=[1,2,3]#给实例p1增加一个数据属性l,存在p1的属性字典里面
    print(Chinese.l)#调用类的数据属性
    print(p1.l)#调用实例的数据属性
    
    C:python35python3.exe D:/pyproject/day24/换个姿势搞你.py
    
    ['a', 'b']
    
    ['a', 'b']
    
    [1, 2, 3]

    6、

    class Chinese:
        country='China'
        l=["a","b"]#存在类的属性字典里面
        def __init__(self,name):
            self.name=name
        def play_ball(self,ball):
            print('%s 正在打 %s' %(self.name,ball))
    p1=Chinese("北爷")
    print(p1.l)#实例p1调用类的数据属性l
    p1.l.append("c")#给实例调用的类的数据属性增加一个c
    print(Chinese.l)
    
    C:python35python3.exe D:/pyproject/day24/换个姿势搞你.py
    
    ['a', 'b']
    
    ['a', 'b', 'c']
  • 相关阅读:
    树状数组BIT
    KMP算法习题(一本通)
    西大附中集训日志
    图论大杂汇
    [IOI2008/BZOJ1791 岛屿](处理基环树的小技巧&基于bfs树形DP)
    字符串知识清单
    BZOJ刷题列表【转载于hzwer】
    NOIP2018复赛游记
    求极大子矩阵的两种方法
    NOIP2002-字串变换【双向BFS】
  • 原文地址:https://www.cnblogs.com/gouguoqilinux/p/9190470.html
Copyright © 2011-2022 走看看