• 类与类之间的关系


    类与类之间的关系
    在我们的世界中事物和事物之间总会有一些联系.
    在面向对象中. 类和类之间也可以产生相关的关系
    1. 依赖关系
    执行某个动作的时候. 需要xxx来帮助你完成这个操作. 此时的关系是最轻的.
    随时可以更换另外一个东西来完成此操作

    2. 关联关系
    在对象里面埋对象
    self.girlFriend = girl
    1. 一对一关系
    你和你的女朋友

    self.teach_list = [t1,t2,t3]
    2. 一对多关系
    一个学校. 一堆老师


    类中的关系: 依赖关系是最轻的. 最重的是继承关系. 关联关系是比较微妙的.

    self到底是谁?
    self:谁调用的就是谁. 类型是根据调用方的对象来进行变换的
    super:表示的是父类

    特殊成员:
    __init__() # 创建对象的时候初始化操作
    __call__() # 对象()
    __getitem__() # 对象[哈哈]
    __setitem__() # 对象[哈哈] = 值
    __new__() # 创建对象的时候.开辟内存
    __enter__() # with 对象
    __exit__() #结束with的时候
    __hash__() # 可哈希 hash()
    依赖关系
    # class Person:
    #     def play(self,tools):
    #         tools.run()
    #         print('很开心,能玩游戏了')
    # class Computer:
    #     def run(self):
    #         print('电脑能运行')
    # class Phone:
    #     def run(self):
    #         print('手机能运行')
    #
    # c = Computer()
    # ph = Phone
    #
    # p = Person()
    # p.play(c)
    
    
    # 植物大战僵尸  特别 low
    # class ZhiWu:
    #     def __init__(self, name, xue, gongji):
    #         self.name = name
    #         self.xue = xue
    #         self.gongji = gongji
    #     def da(self,jiangshi):
    #         print('打%s'% jiangshi)
    #     def diaoxue(self, jiangshi):
    #         self.xue = self.xue - jiangshi
    #         print('植物还有%s血' % self.xue)
    #
    # class JiangShi:
    #     def __init__(self, name, xue, gongji):
    #         self.name = name
    #         self.xue = xue
    #         self.gongji = gongji
    #     def chi(self, zhiwu):
    #         print('吃%s'% zhiwu)
    #     def diaoxue(self, zhiwu):
    #         self.xue = self.xue - zhiwu
    #         print("僵尸还有%s血" % self.xue)
    #
    # zhiwu = ZhiWu('豌豆射手', 200, 50)
    #
    # jiangshi = JiangShi('铁桶僵尸', 500, 60)
    #
    # zhiwu.da(jiangshi.name)
    # jiangshi.diaoxue(zhiwu.gongji)
    # zhiwu.da(jiangshi.name)
    # jiangshi.diaoxue(zhiwu.gongji)
    #
    # jiangshi.chi(zhiwu.name)
    # zhiwu.diaoxue(jiangshi.gongji)
    # jiangshi.chi(zhiwu.name)
    # zhiwu.diaoxue(jiangshi.gongji)
    

      

    关联关系
    # class Boy:
    #     def __init__(self, name, girlFriend = None):
    #         self.girlFriend = girlFriend
    #
    #     def chi(self):
    #         if self.girlFriend:
    #             print(f'带着他的女朋友{self.girlFriend.name}去吃饭')
    #         else:
    #             print('单身狗,也得吃')
    #     def movie(self):
    #         if self.girlFriend:
    #             print(f'带着他的女朋友{self.girlFriend.name}去看电影')
    #         else:
    #             print('单身狗,也得看')
    #
    # class Girl:
    #     def __init__(self, name):
    #         self.name = name
    
    
    
    
    # 学校与老师的列子
    class School:
        def __init__(self, name):
            self.teach_list = []
    
        def zhaopin(self,teach):
            self.teach_list.append(teach)
    
        def shangke(self):
            for i in self.teach_list:
                i.work()
    
    class Teacher:
        def __init__(self,name):
            self.name = name
        def work(self):
            print("%s老师在上课" % self.name)
    
    sch = School("lnh")
    
    t1 = Teacher('牛1')
    t2 = Teacher('牛2')
    t3 = Teacher('牛3')
    t4 = Teacher('牛4')
    t5 = Teacher('牛5')
    t6 = Teacher('牛6')
    t7 = Teacher('牛7')
    
    sch.zhaopin(t1)
    sch.zhaopin(t2)
    sch.zhaopin(t3)
    sch.zhaopin(t4)
    sch.zhaopin(t5)
    sch.zhaopin(t6)
    sch.zhaopin(t7)
    
    sch.shangke()
    

      

    继承关系
    # class Base:
    #     def chi(self):
    #         print('我会吃')
    #
    # # 派生类 => 子类
    # class Foo(Base):  #继承了Base类,Foo类是对Base的一个扩展
    #     def he(self):
    #         print('我会喝')
    #
    # f = Foo()
    # f.chi()
    # f.he()
    
    # class Cat:  # 父类 =>基类 =>超类
    #     def catch_mouse(self):
    #         print('猫可以抓老鼠')
    #
    # class BosiCat(Cat):  # 子类 => 派生类
    #     pass
    #
    
    
    # class Foo:
    #     pass
    # print(hash(Foo))   # 可哈希
    # print(hash(Foo())
    # 我们写好的类,和创建的对象默认都是可哈希的
    
    # 去掉可哈希
    # class Foo:
    #     __hash__ = None  # 当前类的对象不可哈希
    # print(hash(Foo))    # 可哈希
    # print(hash(Foo()))  # unhashable type: 'Foo'
    
    
    
    # class Foo:
    #     def chi(self,food):
    #         print('我爱吃鱼和', food)
    #
    # class Bar:
    #     def chi(self,food):
    #         print('我爱吃肉和', food)
    # dic = {Foo: '鸡蛋', Bar: '香肠'}
    #
    # for k, v in dic.items():
    #     k().chi(v)
    
    # 类名  => 变量名 -> 为了今天作业
    
    # class Base:
    #     def __init__(self, num):
    #         self.num = num
    #     def func1(self):
    #         print(self.num)
    # class Foo(Base):
    #     pass
    # obj = Foo(123)
    # obj.func1()
    
    
    
    # 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()
    
    
    # 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()
    
    
    
    # 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()
    

      

    特殊成员
    #
    class Foo:
        # def __init__(self): # 初始化操作
            # print("我是init,  我是老二")
            # print("初始化操作. 在创建对象的时候自动调用这个方法")
    #
    #     def __new__(cls, *args, **kwargs): # 创建, 它是真正的构造方法,  可以开辟内存
    #         print("我是new. 我是老大")
    #         return object.__new__(cls)
    #
    #
    #     # 为了 对象()
    #     def __call__(self, *args, **kwargs):
    #         print("我是对象()")
    #
    #     # 对象[]
        def __getitem__(self, item):
            print("item=",item)
            print("你执行了__getitem__")
            return "哈哈"
    #
    #     # 对象[key] = value
    #     def __setitem__(self, key, value):
    #         print("key, ", key)
    #         print("value, ", value)
    #
    #     # del lst[1]
    #     def __delitem__(self, key):
    #         print("key=", key)
    #
    #     # with 对象:
    #     def __enter__(self):
    #         print("我是enter")
    #
    #     # with 对象: 代码执行完毕. 最后执行这里
    #     def __exit__(self, exc_type, exc_val, exc_tb):
    #         print("我叫exit")
    #
    #     def __len__(self):
    #         print("我的天哪")
    #         return 3
    #
    #
    f = Foo()    # 自动执行__init__()
    # f() # 调用-> __call__()
    # print(callable(f)) # 对象()
    
    print(f["李嘉诚"]) # 自动调用__getitem__()
    f['jay'] = "林俊杰"
    
    # del f['哈哈']
    
    # with f:
    #     print("我是哈哈哈哈")
    
    # with open() :
    
    
    # lst = ["孙艺珍", "李金珠", "井柏然"]
    #
    # lst[2] # =>自动的调用__getitem__()
    
    
    # def func():
    #     pass
    # func = 3
    # print(callable(func)) # 判断xxx是否是可调用的
    
    #
    # f.__init__()  # 第一次这么写. 以后别这么写
    # lst = [1,2,3,4]
    # it = iter(lst)
    #
    # print(it.__next__())
    #
    # print(next(it)) # __next__()
    
    # 面试之前翻一番
    # 写出15个特殊成员, 并给出具体作用
    
    # class H:
    #     country = "大清"
    #
    # print(H.country)
    
    # 面向对象编程的执行流程 ->
    # 1. 加载类 -> 给类创建一个名称空间 -> 主要存放类变量.
    # 2. 创建对象 -> 先找类. -> 根据类来开辟内存 -> 执行类中的__new__()  -> 执行__init__()  -> 返回对象
    
    
    
    class Student:
        def __init__(self, name, no, gender, cls, age):
            self.name = name
            self.no = no
            self.gender = gender
            self.cls = cls
            self.age = age
    
    
        # 这个对象字符串的表示.
        def __str__(self): # 返回该对象的字符串表示形式
            return f"{self.name}, {self.no}, {self.gender}"
    
        def __repr__(self): # 该对象的官方的字符串表示形式
            return f"{self.name}, {self.no}, {self.gender}"
    
    
    s = Student("董仲浩", "3", "男", "S18", "31")
    print(s)
    

      

  • 相关阅读:
    linux安装kibana
    linux安装6.5.3版本elastic search
    linux非root用户安装nginx
    linux非root用户安装ncurses-devel依赖
    linux无网络情况下安装rpm包
    linux非root用户安装rabbitmq
    linux非root用户安装4.0.14版本redis
    (初)Knockout 监控属性(Observables)
    ECMAScript6
    SonarLint 代码质量管理
  • 原文地址:https://www.cnblogs.com/YangWenYu-6/p/10187617.html
Copyright © 2020-2023  润新知