day17-2018-11-8 类与类之间的关系

# 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)
# class Foo:
#      def __init__(self):
#         pass
#      def method(self):
#         pass
#     # 该类的对象就不可哈希了
#      __hash__ = None
#
#
# # print(hash(Foo)) # 类和对象默认都是可哈希的
# print(hash(Foo())) # unhashable type: 'Foo'

# 做作业的时候能用到
# dic = {Foo: Foo(), Foo():'胡辣汤'}

# s = []
# print(hash(s)) #  列表是不可哈希的

class Base:
    def __init__(self, num):
        self.num = num

    def func1(self):
        print(self.num)
        self.func2()

    def func2(self):
        print(111, self.num)

class Foo(Base):
    def func2(self):
        print(222, self.num)

lst = [Base(1), Base(2), Foo(3)]
for obj in lst:
    obj.func2()


# self:  谁调用的就是谁

# class Car:
#     def run(self):
#         print("我的车会跑")
#
#
# CarCarCar = Car # 类名是变量
#
# c = CarCarCar()
# c.run()
#
#
# def func():
#     print("我是函数")
# func()
#
# fn = func
# fn()

def func():
    print("娃哈哈 ")
func()
# class Car:
#     def __init__(self, color, pai):
#         self.color = color
#         self.pai = pai
#     def __call__(self, *args, **kwargs):
#         print("这里是call")
#
#     def __getitem__(self, item):
#         print("这里是getitem, item=", item)
#
#     def __setitem__(self, key, value):
#         print(key, value)
#
#     def __delitem__(self, key):
#         print(key)
#
#     def __add__(self, other): # 在执行两个对象相加的时候自动调用
#         print("证明我来过")
#     def __enter__(self):
#         print("进来的时候")
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         print("这里是出去")
#
#     def __str__(self): # 当前对象的字符串表示形式
#         return "我的车很厉害"
#
#     def __repr__(self):  #  一个对象的官方字符串表示形式
#         return "我的车非常非常厉害"
#
#     def __iter__(self):
#         return (i for i in range(10))
#
#     # def __hash__(self):
#     #     return hash(self.pai) + hash(self.color)
#     __hash__ = None
#
#
# c = Car("红", "1.5T") # 在创建对象的时候会自动的调用__init__()  类名()  ==> init()
# # c() # 对象()  => __call__()
# # c["马化腾"] # 对象[任意参数] => __getitem__(item)
# # c["胡辣汤"] = "河南开封"
# # del c['上海小混沌大碗']
#
# # c2 = Car("黑色", "1.8T")
# # cc = c+c2
#
# # with c as m: # 装饰器
# #     print("哈哈哈")
# #
# # print("呵呵呵")
# # print(c) # 当打印一个对象的时候. 默认的去执行__str__ 根据__str__返回的结果进行打印
# # print(repr(c))
#
# print("%s" % c) # %s __str__
# print("%r" % c) # %r __repr__
#
# # c > c2 # great than
# # c< c2  # less than
# # c >= c2 # greate and equals
#
# for s in c:
#     print(s)


class Car:
    def __init__(self, color, pai): # 初始化方法
        print("哪有地呀")
        self.color = color
        self.pai = pai

    # 这里才是真正的构造方法
    def __new__(cls, *args, **kwargs):
        print("我的天哪")
        # 固定的返回值
        return object.__new__(cls)

c = Car("红色", "京A66666") # 先执行__new__ 返回object.__new__(cls).把返回的空对象传递给 __init__()

print(c.color)

__call__

__getitem__

 __setitem__

 __add__

__enter__ 和 __exit__

__new__

猜你喜欢

转载自www.cnblogs.com/VastTry/p/9931475.html