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)


特殊成员
__init__()
__call__()
__getitem__()
__setitem__()
__delitem__()
__enter__()
__exit__()

__hash__() hash(c)
__len__() len(c)
__iter__()
__add__() +

__new__() 构造方法. 用来创建对象的. 开辟内存


     特殊成员
      例  # 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)

猜你喜欢

转载自www.cnblogs.com/wanxiangai/p/9931507.html