17 类与类之间的关系以及特殊成员

类与类之间的关系
1. 依赖关系. 通过参数的传递把另外一个类的对象传递进来
2. 关联关系, 组合, 聚合. 通过属性设置来维护两个对象的关系
def __init__():
self.stuList = []
def __init__():
self.teacher = teacher

class DaXiang:
    def open(self, bx): #  这里是依赖关系. 想执行这个动作. 必须传递一个bx
        print("大象高高兴兴走到了冰箱的面向前")
        bx.kai() # 传递来的对象执行kai()动作
        print("大象成功的打开了冰箱门")

    def zhuang(self):
        print("自己走进了冰箱")

    def close(self, bx):
        print("大象要关冰箱门了")
        bx.guan()


class BingXiang:
    def kai(self):
        print("我是冰箱. 我会开门 ")
    def guan(self):
        print("我是冰箱. 我会关门 ")

class DianFanGuo:
    def kai(self):
        print("我是电饭锅 我能开 ")
    def guan(self):
        print("我是电饭锅, 我能关 ")

# bx1 = BingXiang()
# bx2 = BingXiang()
dx = DaXiang()
dfg = DianFanGuo()

dx.open(dfg)
dx.zhuang()
dx.close(dfg)

植物大战僵尸. 创建一个植物. 创建一个僵尸
植物: 名字, 攻击力
僵尸: 名字, 血量
植物可以打僵尸. 僵尸掉血
显示出僵尸挨揍之后的血量

class ZhiWu:
    def __init__(self, name, attack, hp):
        self.name = name
        self.attack = attack
        self.hp = hp

    def fight(self, js):
        js.hp -= self.attack

class JiangShi:
    def __init__(self, name, hp, attack):
        self.name = name
        self.hp = hp
        self.attack = attack

    def chi(self, zhiwu):
        zhiwu.hp -= self.attack

lvluo = ZhiWu("绿萝", 20, 10)
js1 = JiangShi("僵尸一号", 100, 5)
lvluo.fight(js1)
lvluo.fight(js1)
lvluo.fight(js1)

js1.chi(lvluo)
js1.chi(lvluo)
js1.chi(lvluo)
print(lvluo.hp)

print(js1.hp)
依赖关系
# class Boy:
#     def __init__(self, name, girlFriend = None):
#         self.name = name
#         self.girlFriend = girlFriend # 关联关系
#
#     def chi(self): # b
#         if self.girlFriend:
#             self.girlFriend.happy()
#             print("%s和%s在吃饭"  %  (self.name, self.girlFriend.name))
#         else:
#             print("没女朋友吃什么吃")
#
# class Girl:
#     def __init__(self, name):
#         self.name = name
#
#     def happy(self):
#         print("有好吃的就开心")
#
#
# b = Boy("王明")
# g = Girl("肉丝")
# # b对象的girlFriend赋值  g
#
# b.girlFriend = g # 突然天降女朋友
# b.chi()
# b.girlFriend = None
# b.chi()
#

# 老师和学生的关系. 一对多的关系  反过来就是一对一
# class Teacher:
#     def __init__(self, name, lst=None):
#         self.name = name
#         if lst == None: # 判断传递过来的参数是否是空
#             self.lst = []
#         else: # 传递过来的是一个列表
#             self.lst = lst
#
#     # 添加学生
#     def tianjia(self, student):
#         self.lst.append(student)
#
#     def display(self):
#         for s in self.lst: # s 是老师的学生
#             print(s.name)
#
#
# class Student:
#     def __init__(self, num, name, teacher=None):
#         self.name = name
#         self.num = num
#         self.teacher = teacher
#
# t = Teacher("大张伟")
# s1 = Student(1, "郭德纲")
# s2 = Student(2, "岳云鹏")
# s3 = Student(3, "张鹤伦")
# s4 = Student(4, "朱云峰")
#
# t.tianjia(s1) # 把学生添加给老师
# t.tianjia(s2)
# t.tianjia(s4)
#
# t.display()

# class Computer:
#
#     def __init__(self, cpu, memory, yingpan, zhuban, xianshiqi):
#         pass
#
#
# class Cpu:
#     pass
#
# class Memory:
#     pass


# 帖子和评论
class Tie:
    def __init__(self, title, content, author, time, pinglun_lst = None):
        self.title = title
        self.content = content
        self.author = author
        self.time = time
        if pinglun_lst == None:
            self.pinglun_lst = []
        else:
            self.pinglun_lst = pinglun_lst

class Pinglun:
    def __init__(self, name, time, content, fav):
        self.name = name
        self.time = time
        self.content = content
        self.fav = fav





pl1 = Pinglun("UZI", "昨天1", "UZI发出祝福",1888888)
pl2 = Pinglun("xiaohu", "昨天2", "UZI发出祝福",1888888)
pl3 = Pinglun("若风", "昨天3", "UZI发出祝福",1888888)
pl4 = Pinglun("let me", "昨天3", "UZI发出祝福",1888888)
pl5 = Pinglun("长长", "昨天4", "UZI发出祝福",1888888)
pl6 = Pinglun("jackylove", "昨天5", "UZI发出祝福",1888888)
pl7 = Pinglun("mlxg", "昨天6", "UZI发出祝福",1888888)
pl8 = Pinglun("miss", "昨天7", "UZI发出祝福",1888888)

lst = []
lst.append(pl1)
lst.append(pl2)
lst.append(pl3)
lst.append(pl4)
lst.append(pl5)
lst.append(pl6)
lst.append(pl7)
lst.append(pl8)


# 显示帖子的内容.  评论的内容
tie = Tie("S8_IG夺冠. 王思聪怒吃热狗. ", "IG的上单The Shy疯了一样. 一个打5个. 自己人都不放过", "王明","某年某月某一天",lst )
print(tie.content)

# 评论
# print(tie.pinglun_lst)

for pl in tie.pinglun_lst:
    print(pl.content)
关联关系

 

3. 关于继承
self: 谁调用的,. self就是谁

4. 特殊成员
__init__() 初始化方法
__new__() 构造方法
__call__() 对象()
__add__() +
__getitem__ 对象[]
__setitem__ 对象[] = 值
__delitem__ del 对象[]
__del__ del 对象. 析构
__len__ len(对象)
__iter__ for. iter()
__next__ next()
__dir__ dir()
__getslice__ 对象[::]
__hash__ hash()
__class__
__dict__ 查看当前项的属性

class doges(object):
    """类的描述信息"""
    def __init__(self,name,food):
        self.name=name
        self.food=food
        self.data={}#定义一个类的字典
    def __call__(self, *args, **kwargs):#对象后面加括号就执行
         print(*args)
    def __str__(self):#默认输出返回值
         return self.name
    def __repr__(self):
        return self.name
    def __getitem__(self):#可以获取类的的字典
          return self.data
    def __setitem__(self, key, value):#可以设置类的的字典
         self.data[key]=value
    def __delitem__(self, key):#可以删除类的字典的内容
        del self.data[key]
    def __iter__(self):
        return [i for i in range(10)]
    def __len__(self):
        return 6
    def __getattr__(self, item):
        return 1

dog=doges('xxx','iii')
print(dog.__doc__)
b=doges("a","apple")
print(b.__module__)#操作的对象的那个模块
print(dog.__class__)#当前操作的对象的类是什么
dog('111')#
print(doges.__dict__)#查看类或对象的成员  类只打印类的成员不打印对象的成员
print(dog.__dict__)#查看类或对象的成员 对象只打印对象的成员不打印类的成员
print(dog)#打印 __str__的返回值
print(dog.__str__())#打印返回值

dog['1']=1000#触发.__setitem__()
dog['2']=1000#触发.__setitem__()
print(dog.__getitem__())
print(dog.__delitem__('1'))#删除类中字典
print(dog.__getitem__())
print(dog.__dict__)
print(dog.__iter__())  #
print(dog.__len__())
特殊成员
class Foo:
     def __init__(self):
        pass
     def method(self):
        pass
    # 该类的对象就不可哈希了
     __hash__ = None


# print(hash(Foo)) # 类和对象默认都是可哈希的
print(hash(Foo())) # unhashable type: 'Foo'
如何设置类是否可hash

 

posted @ 2018-11-09 17:34  冰底熊  阅读(283)  评论(0编辑  收藏  举报