zoukankan      html  css  js  c++  java
  • 面向对象之类属性实例属性及其增删改查

    1.类相关
    class Chinese():
        pifu = "黄种人"
    
        def chadui():
            print("正在插队!")
    
        def xuanfu(self):
            print("正在炫富")
    
    
    print(Chinese.pifu)   #打印类属性 pifu
    Chinese.chadui()     #类方法
    Chinese.xuanfu(1)  #类得方法 必须传入一个值self  这里可以随便传入 因为 这个函数下面没有用的实例self的属性
    
    print(dir(Chinese))   #打印类中的方法
    print("dict", Chinese.__dict__)  #打印类得属性  字典形式
    
    print(Chinese.__dict__["pifu"])   #获取类字典的key所对应的值
    print(Chinese.__dict__["chadui"]())
    print(Chinese.__dict__["xuanfu"](1))
    2.实例属性的增删改查
    class Chinese(): name = "中国人" def __init__(self, name, age): self.name = name self.age = age def eat_food(self, food): print("%s is eat %s" % (self.name, food)) print(Chinese.name) # 类得属性 print("类属性", Chinese.__dict__) p1 = Chinese("alex", 33) #生成实例 print(p1.age) # 查询实例的属性age p1.sex = "male" #设置实例的属性 sex print("实例属性", p1.__dict__) #查看实例都有哪些属性 # 不要修改底层的属性字典!!!!!!! p1.__dict__['sex'] = 'xiuxgaile' print(p1.__dict__) print(p1.sex)

      

    3.类属性的增删改查
    class Chinese():
        name = "中国人"
    
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        def eat_food(self, food):
            print("%s is eat %s" % (self.name, food))
    
    
    print(Chinese.name)
    Chinese.name = "黄种人"  #修改类得属性
    print(Chinese.name)  # 对类得属性的增删改查
    Chinese.action = "打架"  # 增加类属性
    # print(dir(Chinese))  
    # print(Chinese.action)
    Chinese.food = "屎"
    p1 = Chinese("alex", "33")
    # Chinese.eat_food(p1,Chinese.food)
    print(dir(Chinese))
    p1.eat_food("food")
    del Chinese.food  # 类属性的删除
    print(dir(Chinese))
    
    
    def he_shui(self, water):
        print("%s is drinking %s" %(self.name,water))
    Chinese.heshui=he_shui   # 对类得方法的增加
    
    p1.heshui("shui")
    
    #修改 ,删除相同
    
    4.类属性实例属性的查找顺序
    class Chinese:
        country='China'
        def __init__(self,name):
            self.name=name
    
        def play_ball(self,ball):
            print('%s 正在打 %s' %(self.name,ball))
    p1=Chinese('alex')
    print(p1.country)
    p1.country='日本'  # 这个是对实例的属性的添加
    print('类的--->',Chinese.country)
    print('实例的',p1.country)
    
    
    # *********************************************
    country='中国'
    class Chinese:
        def __init__(self,name):
            self.name=name
    
        def play_ball(self,ball):
            print('%s 正在打 %s' %(self.name,ball))
    p1=Chinese('alex')
    print(p1.country)  #找不到country的值 因为用的是点的方法  那么实例只会先找自己实例属性 找不到找类得属性
                       # 再找不到就报错了
    
    
    # *****************************************************************
    
    
    country='中国'
    class Chinese:
        def __init__(self,name):
            self.name=name
    
        def play_ball(self,ball):
            print('%s 正在打 %s' %(self.name,ball))
    
    def shi_li_hua():
        name=input('>>: ')
        p1=Chinese(name)    #初始化 实例 name 是自己输入的值
        # print(p1.country)  # 找不到country
        print(p1.name) #   实例的name
    shi_li_hua()
    
    # ********************************************************
    
    
    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(Chinese.__dict__)
    print(Chinese.country)
    p1 = Chinese('alex')  # 实例化过程没有掉实例或者类里的country
    # print('实例--------》',p1.country)  #这个找的是类里面的country
    

     

    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('alex')
    print(p1.l)  # 类得l列表
    p1.l.append('c')  # 类l  添加了c
    print(p1.__dict__)
    print(Chinese.l)
    
    # p1.l=[1,2,3]     #实例增加了一个列表l
    # print(Chinese.l)  #类得l
    # print(p1.__dict__)  #实例增加一个l
    

      

     

     

  • 相关阅读:
    Sql Server截断日志(转)
    C#/VB.NET语法的比较(转)
    ReportViewer矩阵报表
    逐步学习 iPhone App 开发(1)
    一月二十四日,无家可归
    再见2009
    poj 1376 机器人广搜
    hdu 1004 颜色问题
    hdu 2734 简单地字符串处理
    1116 贪婪的送礼者
  • 原文地址:https://www.cnblogs.com/zjcode/p/8650192.html
Copyright © 2011-2022 走看看