1. 依赖关系
在方法中引入另一个类的对象 (最不紧密的一种关系)
依赖关系都是把另一个对象当做参数
class Elephant: def __init__(self, name): self.name = name def open(self, ref): print('冰箱开门吧') ref.open_door() def close(self, ref): print('冰箱关门吧') ref.close_door() def get_in(self): print('进冰箱装自己') class Refrigerator: def open_door(self): print('冰箱默默地打开了自己的门') def close_door(self): print('冰箱默默地关上了自己的门') class GaoYaGuo: def open_door(self): print('高压锅打开了自己的盖子') def close_door(self): print('高压锅关上了自己的盖子') e = Elephant('小象') ref1 = Refrigerator() guo = GaoYaGuo() #大象进冰箱 e.open(ref1) e.get_in() e.close(ref1) #大象进了高压锅 e.open(guo) e.get_in() e.close(guo) # 大象与冰箱和高压锅是依赖关系,联系并不紧密,可以更改对象
2. 关联关系, 聚合关系, 组合关系
类与类之间的关系是比较紧密的. a需要b做什么,a中要拥有b
class Boy: def __init__(self, name, character, girlFriend=None): self.name = name self.character = character self.girlFriend = girlFriend def meet(self, girl): # 给girlFriend传值 self.girlFriend = girl def eat(self): if self.girlFriend: # if True 如果有girlFriend print('%s和%s随便吃' % (self.name, self.girlFriend.name)) else: # 如果没有girlFriend print('单身狗不用吃') class Girl: def __init__(self, name): self.name = name def eat(self): print('%s在吃饭' % self.name) girl = Girl('白骨精') alex = Boy('金角大王', '娘') alex.eat() alex.meet(girl) alex.eat() girl.eat() alex.girlFriend.eat()
#一个对多个 class School: # def __init__(self, name, address, tel, teach_list=[]): #teach_list写在这里是默认值参数,可以共用,如果该参数是可以修改的,一旦内容改变,不同对象共用相同内容 def __init__(self, name, address, tel): self.name = name self.address = address self.tel = tel self.__teach_list = [] #每一个对象(学校)都会有一个teach_list,默认是空 # 教师信息不对外展示,私有属性 def hire(self, teacher): self.__teach_list.append(teacher) def display(self): for el in self.__teach_list: print(el.name, el.hobby) class Teacher: def __init__(self, name, gender, salary, hobby, school): self.name = name self.gender = gender self.salary = salary self.hobby = hobby self.school = school oldboy_bj = School("北京老男孩", "美丽富饶的沙河", "10086") oldboy_sh = School("北京老男孩上海分校", "上海浦东", "10010") oldboy_sz = School("北京老男孩深圳分校(骑士计划)", "南山区", "10000") t1 = Teacher("配齐", "男", 200000, "上课", oldboy_bj) t2 = Teacher("太白", "男", 150000, "开车", oldboy_bj) t3 = Teacher("Eggon", "男", 123456, "钻研技术", oldboy_sh) t4 = Teacher("高鑫", "女", 45678, "相夫教子", oldboy_sz) t5 = Teacher("日天", "男", 666, "看天", oldboy_sz) print(t3.school.name, t3.school.address) # 找到老师所在的学校名称和地址 oldboy_bj.hire(t1) oldboy_bj.hire(t2) oldboy_bj.display() oldboy_sh.hire(t3) oldboy_sz.hire(t4) oldboy_sz.hire(t5) oldboy_sz.display()
3. 继承关系, 实现关系
self: 当前执行这个方法的对象
子类在不影响父类的程序运行的基础上对父类进行的扩充和扩展.
父类:超类,基类. 子类:派生类
4. 特殊成员(__init__) (尽量了解)
__init__()
__new__()
__getitem__() obj[xxx]
__setitem__() obj[xxx] = xxxx
__delitem__() del obj[xxx]
__call__() obj()
__str__() print(obj) 返回这个对象的字符串表示形式
__enter__()
__exit__() with
类名和对象是否可以作为key
# 可哈希. 内部是否哈希算法 __hash__ class Foo(object): # 所有的类都会默认继承object def __init__(self): pass def func(self): pass __hash__ = None # 默认的类和对象都是可哈希的,如果加了__hash__ = None,则变为不可哈希 dic = {} dic[Foo] = "123456" # 类名是可哈希的。 dic[Foo()] = "刘伟" # 类中是否包含__hash__ print(dic)
class Foo: def __init__(self): print("我是初始化") def __call__(self, *args, **kwargs): print("我是靠") def __getitem__(self, item): print("我是getitem", item) return "大胖小子" def __setitem__(self, key, value): print(key, value) def __delitem__(self, key): print(key) def __enter__(self): print("我是进入") return "周润发" def __exit__(self, exc_type, exc_val, exc_tb): print("我是出来") # 类名() __init__() 构造方法 obj = Foo() # 对象() __call__() obj() # python特有的. # 对象[xxx] 从对象中获取数据 默认执行__getitem__() print(obj['小明']) # 对象[xxx] = ,.... 默认执行__setitem__() obj["汪峰"] = "章子怡" # del obj[key] 默认执行__delitem__() # del obj['马化腾'] # dic = {"name":'汪峰', 'age':18} # print(dic['name']) # # with obj as xx: # __enter__() __exit__() # print(xx) # print("你好. 我叫周润发")
class Boy(object): def __init__(self, name, address, phone): self.name = name self.address = address self.phone = phone def __str__(self): #打印对象时显示对象内容 而不是显示类中的对象 return "name:%s, address:%s phone:%s" % (self.name, self.address, self.phone) def __new__(cls, *args, **kwargs): print("新概念") return object.__new__(cls) # 这句话才是创建对象. b = Boy("alex", "北京沙河", "10086") print(b)
# __getitem__的用法 #方法一: class DataBase: '''Python 3 中的类''' def __init__(self, id, address): '''初始化方法''' self.id = id self.address = address self.d = {self.id: 1, self.address: "192.168.1.1", } def __getitem__(self, key): # return self.__dict__.get(key, "100") return self.d.get(key, "default") data = DataBase(1, "192.168.2.11") print(data["hi"]) print(data[data.id]) # 方法二 class DataBase: '''Python 3 中的类''' def __init__(self, id, address): '''初始化方法''' self.id = id self.address = address def __getitem__(self, key): return self.__dict__.get(key, "100") data = DataBase(1, "192.168.2.11") print(data["hi"]) print(data["id"]) # 方法三 class STgetitem: def __init__(self, text): self.text = text def __getitem__(self, index): result = self.text[index].upper() return result p = STgetitem("黄哥Python") print(p[0]) print("------------------------") for char in p: print(char) # 示例引用自https://zhuanlan.zhihu.com/p/27661382
class SetAndGet: __dv__ = {} # Dictionary Values def __init__(self, dv={}): self.__dv__ = dv def __getitem__(self, key): return self.__dv__.get(key, None) def __setitem__(self, key, value): self.__dv__[key] = value print('我是setitem') obj = SetAndGet() obj['name'] = 'alex' print(obj.__dv__) print(obj['name'])