小学生绞尽脑汁也学不会的python(面对对象-----类与类之间的关系)

Posted 孔辉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小学生绞尽脑汁也学不会的python(面对对象-----类与类之间的关系)相关的知识,希望对你有一定的参考价值。

小学生绞尽脑汁也学不会的python(面对对象-----类与类之间的关系

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("北京老男孩", "美丽富饶的沙河", "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.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. 继承关系, 实现关系
self:当前执行这个方法的对象。

# 可哈希. 内部是否哈希算法 __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()

# 1
# 111 1
# 2
# 111 2
# 3
# 222 3

# 总结: self当前访问xx方法的那个对象

  


4. 特殊成员(__init__)

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

# 对象[xxx] = ,.... 默认执行__setitem__()
# obj["汪峰"] = "章子怡"

# del obj[key] 默认执行__delitem__()
# del obj[‘马化腾‘]



# dic = {"name":‘汪峰‘, ‘age‘:18}
# print(dic[‘name‘])
#
# with obj as xx:
#     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)




lst = [123,456]
print(lst)

  






以上是关于小学生绞尽脑汁也学不会的python(面对对象-----类与类之间的关系)的主要内容,如果未能解决你的问题,请参考以下文章

看录像~有视频也学不会前端开发。为啥?

总也学不会Linux命令行?这本新书大概率能拯救你!

大学的计算机专业学生怎么学习

python面对对象

Python - 面对对象(基础)

python支持面向对象吗?