类、增删改查类、实例的增删改查

#
# 1、数据属性
# 2、函数属性
class Chinese:
    '定义一个中国人的类'
    dang = "共党"
    # def __init__(name,age,gender):
    #     dic = {
    #             "name":name,
    #             "age":age,
    #             "gender":gender
    #     }
    #     return dic

    def __init__(self,name,age,gender): # 初始化函数
        self.mingzi=name
        self.nianlin=age
        self.xinbei=gender

    def suidi():
        print("随地")
    def chadui(self):
        print("%s插队"%self.mingzi)

# 实例化

# print(Chinese.dang)   #  共党
# Chinese.suidi()       #  随地
# Chinese.chadui("asd") #  插队
# print(dir(Chinese)) # 查看列表格式类功能
# print(Chinese.__dict__)  # 查看类属性
# print(Chinese.__dict__["dang"]) #  共党
# Chinese.__dict__["suidi"]()  # 随地
# Chinese.__dict__["chadui"](1) # 插队

p1 = Chinese("",18,"feml")
print(p1.__dict__)
# {'mingzi': '元', 'nianlin': 18, 'xinbei': 'feml'}
print(p1.mingzi)  #
Chinese.chadui(p1)  #  元插队

 

# 类的增删改查
class
Chinese: country = "China" def __init__(self,name): self.name = name def play(self): print("打开") # Chinese.country="japan" # print(Chinese.country) # japan # 传参 self = "alex" p1 = Chinese("alex") print(p1.__dict__) # {'name': 'alex'} print(p1.country) # japan # 增加 Chinese.dang = "共党" print(Chinese.dang) # 共党 print(p1.dang) # 共党 # 删除 del Chinese.dang del Chinese.country # 添加函数 def eat_food(self,food): print("正在吃%s" %food) Chinese.eat=eat_food print(Chinese.__dict__) # {'__module__': '__main__', '__init__': <function Chinese.__init__ at 0x0000000001E73C80>, 'play': <function Chinese.play at 0x0000000001E73D08>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None, 'eat': <function eat_food at 0x00000000003B2EA0>} p1.eat("12") # 正在吃12 def test(self): print("test") Chinese.play_ball = test p1.play_ball() # test Chinese.play_ball(p1) # test

 

# 实例增删改查
class Chinese:

    country = "China"

    def __init__(self,name):
        self.name = name

    def play(self,ball):
        print("%s打开%s"%(self,name,ball))

p1 = Chinese("lei")
print(p1.__dict__)  # {'name': 'lei'}

# 查看
print(p1.name)   #  lei
print(p1.play)   #  <bound method Chinese.play of <__main__.Chinese object at 0x0000000001E77780>>

# 增加
p1.age = 18
print(p1.__dict__)  # {'name': 'lei', 'age': 18}

# 不要做的修改方式
# p1.__dict__["sex"] = "male"
# print(p1.__dict__)  #  {'name': 'lei', 'age': 18, 'sex': 'male'}
# print(p1.sex)       #  male

# 修改
p1.age = 19
print(p1.age)  #  19

# 删除
del p1.age
print(p1.__dict__)  #  {'name': 'lei'}

 

# class Chinese:
#
#     country = "China"
#
#     def __init__(self,name):
#         self.name = name
#
#     def play(self,ball):
#         print("%s打开%s"%(self,name,ball))

# p1 = Chinese("lei")
# print(p1.country)  #  China
# p1.country = "japen"
# print("类",Chinese.country)  # 类 China
# print("实例",p1.country)     # 实例 japen

country = "China"
class Chinese:
    country = "中国"
    l = ["a","b"]
    def __init__(self,name):
        self.name = name
        print(">>>",country)

    def play(self,ball):
        print("%s打开%s"%(self,name,ball))

p1 = Chinese("lei")  # >>> China
print(p1.country)     #  中国


print(p1.l)  #  ['a', 'b']
p1.l.append("c")
print(p1.__dict__)  # {'name': 'lei'}
print(Chinese.l)    #  ['a', 'b', 'c']
  

 

posted @ 2018-11-22 21:12  文昌  阅读(307)  评论(0)    收藏  举报