zoukankan      html  css  js  c++  java
  • python3.6入门到高阶(全栈) day017 类与类的关系

    今日主要内容
    类与类之间的关系
    依赖关系
    在方法中给方法传递一个对象. 此时类与类之间的关系是最轻的

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

    def fight(self, js): # 这里是依赖关系. 想执行这个动作. 必须传递一个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)

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

    print(js1.hp)


    关联关系(组合, 聚合)
    def __init__(self, name, xxxList =None):
    self.xxxList = xxxList

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

    一对多. 一的一方埋集合
    多的一方埋实体

    例 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()

    简单的继承
    self: 谁调用的. self就是谁


    创建对象的真正步骤:

    例 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)


    特殊成员方法

    
    

      __new__()  构造方法, 在创建对象内存时调用.

    
    

      __init__()  初始化方法, 往创建好的对象内存里存数据

    
    

      __call__()  对象()时会被调用.

    
    

      __dict__()      将类或者对象内存空间里的全部属性打包成字典的形式.

    
    

        类: 有各种内部属性, 内部方法和一些静态字段, 方法之类的.

    
    

        对象: 在自己的内存空间里只有自己的字段.

    
    

      __str__()  在打印一个对象时默认会执行__str__()方法, 根据返回的结果进行打印.

    
    

      __int__()   数据int()调用

    
    

      __repr__()  一个字符串的官方表示形式

    
    

      __getitem__()  对象[key]自动调用

    
    

      __setitem__()  对象[key] = value自动调用

    
    

      __delitem__()  del  对象[key]时调用

    
    

      __enter__()  with打开文件时调用

    
    

      __exit__()  with关闭文件时调用

    
    

      __add__()  两个对象相加

    
    

      __len__()  len(对象)时调用

    
    

      __iter__()  for循环时调用

    
    

      __hash__()  哈希时调用

    
    

      __lt__()     小于

    
    

       __gt__()  大于

    
    

      __le__()  小于等于

    
    

      __ge__()  大于等于

    
    

      __del__()       析构(删除清空对象)




         特殊成员
          例  # 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)
  • 相关阅读:
    POJ 1739 Tony's Tour(插头DP)
    POJ 1741 Tree(树的分治)
    POJ 1655 Balancing Act(求树的重心)
    POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
    codeforces 359E Neatness(DFS+构造)
    codeforces 295C Greg and Friends(BFS+DP)
    codeforces 228E The Road to Berland is Paved With Good Intentions(2-SAT)
    Eclipse 代码提示功能设置。
    svn 清空
    android 线程
  • 原文地址:https://www.cnblogs.com/wanxiangai/p/9931507.html
Copyright © 2011-2022 走看看