一、类与类之间的关系
    
    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 jin(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("冰箱陌陌的关上了自己的门 ") alex = Elephant("李杰")
bx1 = Refrigerator() #
alex.open(bx1)
alex.jin()
alex.close(bx1)
object

2、关联关系

 # class Boy:
# def __init__(self, name, xingge, girlFriend=None):
# self.name = name
# self.xingge = xingge
# self.girlFriend = girlFriend
#
# def yujian(self, girl):
# self.girlFriend = girl
#
# def chi(self):
# if self.girlFriend:
# print("随便池! %s 和 %s" % (self.name, self.girlFriend.name))
# else:
# print("单身狗, 池什么池?")
#
# class Girl:
# def __init__(self, name, boyFriend):
# self.name = name
# self.boyFriend = boyFriend
# def chi(self):
# print("%s在吃饭" % self.name)
#
# girl = Girl("白骨精")
#
# alex = Boy("金王", "娘")
# alex.chi()
#
# alex.yujian(girl)
# alex.chi()
#
# # 找到alex的女朋友
# # alex.girlFriend.name
# alex.girlFriend.chi() # 一个对多个. class School:
def __init__(self, name, address, phone):
self.name = name
self.address = address
self.phone = phone
self.__teach_list = [] def zhaopin(self, t):
self.__teach_list.append(t) 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("北京老男孩", "美丽富饶的沙河", "")
oldboy_sh = School("北京老男孩, 上海分校", "上海浦东", "")
oldboy_sz = School("北京老男孩, 深圳分校(骑士计划)", "南山区", "") 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.address) # 找到老师所在的学校的地址 oldboy_bj.zhaopin(t1)
oldboy_bj.zhaopin(t2)
oldboy_bj.display() oldboy_sh.zhaopin(t3) oldboy_sz.zhaopin(t4)
oldboy_sz.zhaopin(t5) oldboy_sz.display()

3、类名与对象是否可作为key

 # 可哈希. 内部是否哈希算法 __hash__

 # class Foo(object): # 所有的类都会默认继承object
# def __init__(self):
# pass
# def func(self):
# pass
# __hash__ = None
#
# dic = {}
# dic[Foo] = "123456" # 类名是可哈希的。
# dic[Foo()] = "刘伟" # 类中是否包含__hash__
# print(dic) # 默认的类和对象都是可哈希的 # class Base:
# def __init__(self, num):
# self.num = num
#
# def func1(self):
# print(self.num)
#
# class Foo(Base):
# pass
#
# obj = Foo(123)
# obj.func1() # 123 # class Base:
# def __init__(self, num):
# self.num = num
# def func1(self):
# print(self.num)
#
# class Foo(Base):
# def func1(self):
# print("Foo. func1", self.num)
#
# obj = Foo(123)
# obj.func1() # ???? Foo. func1 123 #
# class Base:
# def __init__(self, num):
# self.num = num
# def func1(self):
# print(self.num)
# self.func2()
# def func2(self):
# print("Base.func2")
#
# class Foo(Base):
# def func2(self):
# print("Foo.func2")
#
# obj = Foo(123)
# obj.func1() # 123 # 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() # 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.func1() #
# 111 1
#
# 111 2
#
# 222 3 # 总结: self当前访问xx方法的那个对象

最新文章

  1. CORS解决ajax跨域
  2. tomcat学习笔记一:安装和配置
  3. python启动服务器
  4. dede调用文章的栏目,作者,发布时间.以及连接数据库输出一个表
  5. vmware 下的linux的host only上网配置
  6. Java 第一天
  7. JFreeChart 图表生成实例(饼图、柱状图、折线图、时序图)
  8. 2014-10 u-boot 顶层config.mk分析
  9. 重写session
  10. Painting The Wall 期望DP Codeforces 398_B
  11. import cv2出现“ImportError: DLL load failed: 找不到指定的模块”
  12. awk 中的难懂符号解释
  13. PYTHON 实现的微信跳一跳【辅助工具】仅作学习
  14. Python实现快速排序--数据结构
  15. 拦截器实现HandlerInterceptor没有提示实现里面的方法
  16. Android 开发 HandlerThread详解 转载
  17. overture里设置踏板标记
  18. 关于Q-LEARNING的优化
  19. [HNOI 2018]毒瘤
  20. 利用javascript:void(0)制作假的提交按钮替代button

热门文章

  1. idea下使用lombok
  2. WPF禁止拖拽窗口到边缘自动最大化
  3. MySQL LOCK TABLES 与UNLOCK TABLES
  4. python里的字典和集合
  5. 走进JDK(三)------AbstractStringBuilder、StringBuffer、StringBuilder
  6. 怎么让挨着的两input之间没有空隙?
  7. SAX vs. DOM (Event vs. Tree)
  8. CImageList
  9. 2.2.9静态同步synchronized方法与synchronized(class)代码块
  10. (转).net面试题(老赵)