zoukankan      html  css  js  c++  java
  • 类属性的增删改查,类属性和实例属性

    1.类属性的增删改查:

    class Chinese: #以下均是属性
        country = "China"
        def __init__(self,name):
            self.name = name
    
        def play_bsktball(self,bsktball):
            print("%s 正在打 %s"%(self.name,bsktball))
    #类查看
    print(Chinese.country)
    
    #类中修改数据属性
    #类修改
    Chinese.country = "ChinaChina"
    p1 = Chinese("ALEX") #声明类实例
    print(p1.__dict__)
    print(p1.country)
    
    #类增加
    Chinese.dang = "dang"
    print(Chinese.dang)#调用dang的三种方式
    print(p1.dang)
    print(p1.__dict__) #类中只含有一个属性"name":"ALEX" 实例基于引用,先从自身字典找,其次从类字典中找
    print(Chinese.__dict__["dang"])
    
    #类删除
    del Chinese.dang
    del Chinese.country
    print(Chinese.__dict__)
    
    #类中修改函数属性
    #增加函数属性
    def eat_food(self,food):
        print("%s 正在吃 %s"%(self.name,food))
    Chinese.eat = eat_food
    print(Chinese.__dict__)
    p1.eat("noodle")
    
    #修改函数属性
    def test(self,play_bsktball):
        print("修改函数%s中内容" %play_bsktball)
    
    Chinese.play_bsktball = test
    p1.play_bsktball("paly_bsktball")

     2.实例属性的增删改查

    class Chinese: #以下是实例属性的增删改查
        country = "China"
        def __init__(self,name):
            self.name = name
        def play_bsktball(self,bsktball):
            print("%s 正在打 %s"%(self.name,bsktball))
    p1 = Chinese("alex") #定义类
    print(p1.__dict__) #输出实例字典
    
    #查看
    print(p1.name)
    print(p1.play_bsktball)
    p1.play_bsktball("bsktball")
    
    #增加
    p1.age=18
    print(p1.age)
    print(p1.__dict__)
    print(p1.age)
    print(Chinese.__dict__) #类中没有实例key,value
    #实例只有数据属性,因为实例出发__init__,所以只能引用__init__中的内容。
    
    #调用实例函数属性的两种方法
    def test(self):
        print("我是来自实例p1的函数属性")
    def test1():
        print("我是来自实例p1实例的函数属性")
    p1.test=test
    p1.test1=test1
    print(p1.__dict__)
    p1.test(p1)
    p1.test1()
    
    #不要修改底层的属性字典
    p1.__dict__["sex"]="male"
    print(p1.__dict__)
    print(p1.sex)
    
    #修改
    p1.age=19
    print(p1.age)
    print(p1.__dict__)
    
    #删除
    del p1.age
    print(p1.__dict__)

    3.定义一个类,等同定义一个作用域

    #定义一个类,只当一个作用域去用,类似于C语言中的结构体
    class MyData:
        pass
    x=10
    y=20
    MyData.x=1
    MyData.y=2
    print(x,y)
    print(MyData.x,MyData.y)
    print(MyData.x+MyData.y)

    4.对象与实例属性(区分用“.”调用和用普通变量调用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("alex")#实例化p1
    print(p1.country)#访问类中country
    print(p1.name)#“name”:"alex" 访问实例中name
    p1.country="日本人"
    print(Chinese.country) #China
    print(p1.country) #日本人
    
    class Chinese:
        country="country"
        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=country,那么输出country
    
    #类中设定作用域有要求,要加“.”,如果不加,只代表简单的变量
    country= "country"
    class Chinese:
        def __init__(self,name):
            self.name=name
            print("--->",country)
    
        def play_ball(self,ball):
            print("%s 正在打 %s"%(self.name,ball))
    p1=Chinese("alex")
    
    #区分调用变量时,加点与不加点的调用方式
    country="china----outside"
    class Chinese:
        country = "中国"
        def __init__(self,name):
            self.name=name
            print("----->",country)
        def play_ball(self,ball):
            print("%s 正在打 %s"%(self.name,ball))
    print(Chinese.__dict__)
    p1=Chinese("alex")#区分带“.”输出为普通变量输出,
    print(Chinese.country)
    print(p1.country)

    5.类与实例属性(区分用“.”调用和用普通变量调用2,结合列表)

    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)
    p1.L=[1,2,3] #向实例中添加[1,2,3]
    print(Chinese.L) #输出仍然是[a,b]
    print(p1.__dict__)
    
    p1.L.append("c")
    print(p1.__dict__)#p1没有变
    print(Chinese.L)#变的是累类中的L

     6.类属性+类实例属性例子1(from 老男孩作业day024)

    #------------------------------------------------------------------类属性增删改查
    class School: #数据属性供函数属性引用
        defi = "这是学校类"
        def __init__(self,name,position,type):
            self.name = name
            self.posi = position
            self.type = type
        def geo_position(self):
            # print("%s学校在"%(self.name,self.position))
            print("%s学校在%s" % (self.name,self.posi))
        def zhiwei_name(self,principal,president):
            principal = principal
            president = president
            print("nit自2018年起,%s的校长是%s,%s的书记是%s" %(self.name,president,self.name,president))
        def enrollment(self,enrol_mon,number):
            print("%s每年在%s招生学生%s" %(self.name,enrol_mon,number))
    print(School.defi)
    print(School.__dict__)
    
    #
    p1=School("nit","宁波","2500")
    print(p1.__dict__)
    p1.geo_position() # School("1","2","3")
    School.position()
    School.zhiwei_name()
    p1.zhiwei_name("章献民","胡征宇")
    
    #数据属性
    #
    School.tuition_fee = "5000-6000"
    print(School.__dict__)
    print(p1.tuition_fee)
    
    #
    p1.tuition_fee = "6000"
    print(School.tuition_fee)
    
    #
    print(p1.__dict__)
    print(School.__dict__)
    del School.tuition_fee
    del p1.tuition_fee
    print(p1.__dict__)
    
    #函数属性
    #增加函数属性
    def add_znj(name,age,type):
        print("%s是%s,2019年%s岁"%(name,age,type))
    School.znj = add_znj
    print(School.znj)
    School.znj("znj","19","female")
    
    #修改函数属性
    def change_znj(name,age,type):
        print("------这是修改后znj的info-------")
        print("%s是%s,2019年%s岁" %(name,age,type))
    School.znj = change_znj
    School.znj("znj","18","female")
    print(School.__dict__)
    
    #删除函数属性内容
    if School.__dict__["znj"]:
        print("True")
    del School.znj
    
    #---------------------------------------------------------------实例属性增删改查
    class School: #数据属性供函数属性引用
        defi = "这是学校类"
        def __init__(self,name,position,type):
            self.name = name
            self.posi = position
            self.type = type
        def geo_position(self):
            # print("%s学校在"%(self.name,self.position))
            print("%s学校在%s" % (self.name,self.posi))
        def zhiwei_name(self,principal,president):
            principal = principal
            president = president
            print("nit自2018年起,%s的校长是%s,%s的书记是%s" %(self.name,president,self.name,president))
        def enrollment(self,enrol_mon,number):
            print("%s每年在%s招生学生%s" %(self.name,enrol_mon,number))
    p1 = School("nit","ningbo","public") #定义类
    print(p1.__dict__) #输出实例字典
    
    #查看
    print(p1.name)
    print(p1.geo_position())
    p1.zhiwei_name("zxm","hzy")
    
    #增加
    p1.plan="快速融入浙大五位一体校区"
    print(p1.plan)
    print(p1.__dict__)
    print(School.__dict__) #类中没有实例key,value
    #实例只有数据属性,因为实例从__init__出发,所以只能引用__init__中的内容。
    
    #修改
    p1.plan="加快浙大宁波五位一体校区建设"
    print(p1.plan)
    print(p1.__dict__)
    
    #删除实例中的key,value
    del p1.plan
    print(p1.__dict__)
    
    #调用实例函数属性的两种方法
    def test(self):
        print("znj在发视频过来")
    def test1(self):
        print("等一下再回复znj")
    p1.test=test
    p1.test1=test1
    print(p1.__dict__,p1.test1)
    p1.test(p1)
    p1.test1(p1)
  • 相关阅读:
    poj 3243 Clever Y(BabyStep GiantStep)
    poj 2417 Discrete Logging
    poj 3481 Double Queue
    hdu 4046 Panda
    hdu 2896 病毒侵袭
    poj 1442 Black Box
    hdu 2815 Mod Tree
    hdu 3065 病毒侵袭持续中
    hdu 1576 A/B
    所有控件
  • 原文地址:https://www.cnblogs.com/yuyukun/p/10582583.html
Copyright © 2011-2022 走看看