zoukankan      html  css  js  c++  java
  • 面向对象 类与类之间的关系 (依赖,组合,聚合,等)

    -----------------------------困难是一位严厉的老师,他产生于克服困难的努力中。
    # ---------------------------------------------------------------------------------#

    # class Page:
    # def __init__(self,lst,pagesize):
    # self.lst =lst
    # self.pagesize = pagesize
    #
    # def start(self,a):
    # return a
    #
    # def end(self,b): #[返回最后一页]
    # return b
    # def index(self,x): #[返回指定页面]
    # return x
    #
    # ye = input("输入要查询的数据的页码-->")
    # ---------------------------------------------------------------------------------#
    #
    #
    # class Elphent:
    # def open(self,bx):
    # print("走到冰箱")
    # self.kai
    #
    # def zhuang(self):
    # print()
    #
    # def guan(self,bx):
    # print()
    #
    # class Bxiang:
    # def kai(self):
    # print("开!")
    # def guan(self):
    # print("关!")
    #
    # dx=Elphent()
    # dx.open()
    # dx.kai
    # ---------------------------------------------------------------------------------#
    # class Plant:
    # def __init__(self, name, atk):
    # self.name = name
    # self.atk = atk
    #
    # def killzom(self):
    # print("植物%s攻击了僵尸%s" % (self.name,munaiyi.name))
    #
    # class Zombie:
    # def __init__(self,name,hp):
    # self.name=name
    # self.hp=hp
    #
    # def re_hp(self):
    # print("僵尸受到伤害掉血")
    #
    # xiangrikui = Plant("小葵花",20)
    # munaiyi =Zombie("木乃伊",1000)
    #
    # while 1:
    # xiangrikui.killzom()
    # print(munaiyi.re_hp()-xiangrikui.atk)

    # ---------------------------------------------------------------------------------#
    # class Wufan():
    # def __init__(self,caiming,tangming,canju=None):
    # self.caiming=caiming
    # self.tangming =tangming
    # self.canju = canju
    #
    # def chi(self):
    # if self.canju:
    # self.canju.addmore()
    # print("吃!")
    # else:
    # print("吃个铲铲")
    #
    # class kuaizi:
    # def __init__(self,name):
    # self.name = name
    #
    # def addmore(self):
    # print("再来一双")
    #
    # pig = Wufan("隆江猪脚饭","紫菜蛋花")
    # kz=kuaizi("筷子")
    #
    # pig.canju = kz
    # pig.chi()
    # pig.canju = None
    # pig.chi()

    # ---------------------------------------------------------------------------------#
    # class Teach:
    # def __init__(self, name, nist=None):
    # self.name = name
    # if nist == None: #判断传递过来的参数是否是空
    # self.nist = []
    # else: #传递过来的是一个列表
    # self.nist = nist
    #
    # #执行添加学生操作
    # def add(self, student):
    # self.nist.append(student)
    # pass
    #
    # def display(self):
    # for s in self.nist: #s 是老师的学生
    # print(s.name)
    #
    #
    # class Students:
    # def __init__(self, num, name, teacher=None):
    # self.num = num
    # self.name = name
    # self.teacher = teacher
    #
    # t =Teach("王大大")
    # s1 = Students(1,"康师傅")
    # s2 = Students(2,"康帅傅")
    # s3 = Students(3,"康大师傅")
    # s4 = Students(4,"康康大冒险")

    # ---------------------------------------------------------------------------------#
    # 1. 看代码写结果;

    class F3(object):

    def f1(self):
    ret = super().f1() # super 执行下一个操作
    print(ret)
    return 123


    class F2(object):
    def f1(self):
    print('123')


    class F1(F3, F2):
    pass


    obj = F1()
    obj.f1() # --> 123 123


    # ---------------------------------------------------------------------------------#
    # 2. 看代码写结果

    class F1(object):
    def __init__(self, a1):
    self.a1 = a1

    def f2(self, arg):
    print(self.a1, arg)


    class F2(F1):
    def f2(self, arg):
    print('666')


    obj_list = [F1(1), F2(2), F2(3)]

    for obj in obj_list:
    obj.f2("喜羊羊")

    # F1(1) --> 1 喜羊羊
    # F2(2) --> 666
    # F3(3) --> 666


    # ---------------------------------------------------------------------------------#
    # 3.看代码写结果

    class F1(object):
    def __init__(self, a1):
    self.a1 = a1

    def f2(self, arg):
    print(self.a1, arg)


    class F2(F1):
    def f2(self, arg):
    print('666')


    obj_list = [F1(1), F2(2), F2(3)]

    for obj in obj_list:
    print(obj.f2("喜羊羊"))

    # F1(1) --> 1,喜羊羊
    # F2(2) --> 666
    # F2(3) --> 666

    # ---------------------------------------------------------------------------------#
    # 4.看代码写结果

    class F1(object):
    def __init__(self,num):
    self.num = num

    def func(self,request):
    print(self.num,request)

    def run(self):
    self.func(999)

    class F2(F1):
    def func(self,request):
    print(666,self.num)

    objs = [F1(1),F2(2),F2(3)]
    objs[1].run() #==> F2(2).run() --> 666 2
    objs[2].run() #==> F2(3).run() --> 666 3


    # ---------------------------------------------------------------------------------#
    # 5.看代码写结果

    class UserInfo(object):
    pass

    class Department(object):
    pass

    class StarkConfig(object):
    def __init__(self, num):
    self.num = num
    def changelist(self, request):
    print(self.num, request)
    def run(self):
    self.changelist(999)

    class RoleConfig(StarkConfig):
    def changelist(self, request):
    print(666, self.num)

    class AdminSite(object):

    def __init__(self):
    self._registry = {}

    def register(self, k, v): #UserInfo #StarkConfig
    self._registry[k] = v(k) #self._registry[UserInfo] = StarkConfig(UserInfo)

    site = AdminSite()
    site.register(UserInfo, StarkConfig) # self._registry = {UserInfo,StarkConfig}

    site.register(Department, RoleConfig) # self._registry = {Department,RoleConfig}

    for k, row in site._registry.items():
    row.run()

    # ---------------------------------------------------------------------------------#
    # 6.看代码写结果

    class UserInfo(object):
    pass

    class Department(object):
    pass

    class StarkConfig(object):
    def __init__(self, num):
    self.num = num

    def get_vals(self):
    v = [11, 22, 33]
    extra = self.extra_vals()
    if extra:
    v.extend(extra)
    return v
    def extra_vals(self):
    pass

    def run(self):
    return self.get_vals()

    class RoleConfig(StarkConfig):
    def extra_vals(self):
    return [99, 88]

    class AdminSite(object):
    def __init__(self):
    self._registry = {}
    def register(self, k, v):
    self._registry[k] = v(k)

    site = AdminSite()
    site.register(UserInfo, StarkConfig)
    site.register(Department, RoleConfig)
    for k, row in site._registry.items():
    print(row.run())

    # -- > 同第5题
    # ---------------------------------------------------------------------------------#

    # 7. 背写你了解的所有特殊方法并附示例;



    # ---------------------------------------------------------------------------------#

    # 8. 看代码写结果;

    class StarkConfig(object):
    def __init__(self, num):
    self.num = num
    def __call__(self, *args, **kwargs):
    print(self.num)

    class RoleConfig(StarkConfig):
    def __call__(self, *args, **kwargs):
    print(self.num)

    v1 = StarkConfig(1)
    v2 = RoleConfig(11)
    v1() #--> 1 , 11
    v2() # --> 11

    # ---------------------------------------------------------------------------------#

    # 9. 看代码写结果;

    class StarkConfig(object):
    def __init__(self, num):
    self.num = num

    def run(self):
    self()

    def __call__(self, *args, **kwargs):
    print(self.num)

    class RoleConfig(StarkConfig):

    def __call__(self, *args, **kwargs):
    print(345)

    v1 = StarkConfig(1)
    v2 = RoleConfig(11)
    print(v1.run()) # 1
    print(v2.run()) # 11 345



    # ---------------------------------------------------------------------------------#
    # 10. 看代码写结果。

    class StarkConfig(object):
    def __init__(self, num):
    self.num = num
    def run(self):
    self()
    def __call__(self, *args, **kwargs):
    print(self.num)

    class RoleConfig(StarkConfig):

    def __call__(self, *args, **kwargs):
    print(345)
    def __getitem__(self, item): #wupeiqi
    return self.num[item]

    v1 = RoleConfig('alex')
    v2 = StarkConfig("wupeiqi")
    print(v1[1]) #--> 345 alex
    print(v2[2]) #--> none 或者error



    # ---------------------------------------------------------------------------------#
    # 11. (升级题)
    '''
    1.完成学生选课系统
    学生选课
    学生:
    信息:学号,姓名,住址选的课程列表,
    功能:
    查看:查看该学生所有课程信息
    添加课程:把选好的课程添加到课程列表中(传参)
    课程:
    信息: 课程编号,课程名称,老师
    功能:
    查看:查看该课程的全部信息
    设置老师:给当前课程设置一个老师.(传参)
    老师:
    信息:老师编号,老师名称,老师电话
    功能:无

    请完成以上三个类.创建6个课程,6个老师.给课程安排好老师.然后创建30个学生
    每个学生随机被分配3个课程.最终显示出这三十个学生的选课情况以及任课老师的电话

    '''

    class student:
    def __init__(self,nom,name,adress):
    pass
    def skill(self,read,add_lesson):
    pass

    class lesson:
    def __init__(self,nom,leson_name,teacher):
    pass

    class Teacher:
    def __init__(self,nom,name,phone):
    pass







    # class Page:
    # def __init__(self,lst,pagesize):
    # self.lst =lst
    # self.pagesize = pagesize
    #
    # def start(self,a):
    # return a
    #
    # def end(self,b): #[返回最后一页]
    # return b
    # def index(self,x): #[返回指定页面]
    # return x
    #
    # ye = input("输入要查询的数据的页码-->")
    # ---------------------------------------------------------------------------------#
    #
    #
    # class Elphent:
    # def open(self,bx):
    # print("走到冰箱")
    # self.kai
    #
    # def zhuang(self):
    # print()
    #
    # def guan(self,bx):
    # print()
    #
    # class Bxiang:
    # def kai(self):
    # print("开!")
    # def guan(self):
    # print("关!")
    #
    # dx=Elphent()
    # dx.open()
    # dx.kai
    # ---------------------------------------------------------------------------------#
    # class Plant:
    # def __init__(self, name, atk):
    # self.name = name
    # self.atk = atk
    #
    # def killzom(self):
    # print("植物%s攻击了僵尸%s" % (self.name,munaiyi.name))
    #
    # class Zombie:
    # def __init__(self,name,hp):
    # self.name=name
    # self.hp=hp
    #
    # def re_hp(self):
    # print("僵尸受到伤害掉血")
    #
    # xiangrikui = Plant("小葵花",20)
    # munaiyi =Zombie("木乃伊",1000)
    #
    # while 1:
    # xiangrikui.killzom()
    # print(munaiyi.re_hp()-xiangrikui.atk)

    # ---------------------------------------------------------------------------------#
    # class Wufan():
    # def __init__(self,caiming,tangming,canju=None):
    # self.caiming=caiming
    # self.tangming =tangming
    # self.canju = canju
    #
    # def chi(self):
    # if self.canju:
    # self.canju.addmore()
    # print("吃!")
    # else:
    # print("吃个铲铲")
    #
    # class kuaizi:
    # def __init__(self,name):
    # self.name = name
    #
    # def addmore(self):
    # print("再来一双")
    #
    # pig = Wufan("隆江猪脚饭","紫菜蛋花")
    # kz=kuaizi("筷子")
    #
    # pig.canju = kz
    # pig.chi()
    # pig.canju = None
    # pig.chi()

    # ---------------------------------------------------------------------------------#
    # class Teach:
    # def __init__(self, name, nist=None):
    # self.name = name
    # if nist == None: #判断传递过来的参数是否是空
    # self.nist = []
    # else: #传递过来的是一个列表
    # self.nist = nist
    #
    # #执行添加学生操作
    # def add(self, student):
    # self.nist.append(student)
    # pass
    #
    # def display(self):
    # for s in self.nist: #s 是老师的学生
    # print(s.name)
    #
    #
    # class Students:
    # def __init__(self, num, name, teacher=None):
    # self.num = num
    # self.name = name
    # self.teacher = teacher
    #
    # t =Teach("王大大")
    # s1 = Students(1,"康师傅")
    # s2 = Students(2,"康帅傅")
    # s3 = Students(3,"康大师傅")
    # s4 = Students(4,"康康大冒险")

    # ---------------------------------------------------------------------------------#
    # 1. 看代码写结果;

    class F3(object):

    def f1(self):
    ret = super().f1() # super 执行下一个操作
    print(ret)
    return 123


    class F2(object):
    def f1(self):
    print('123')


    class F1(F3, F2):
    pass


    obj = F1()
    obj.f1() # --> 123 123


    # ---------------------------------------------------------------------------------#
    # 2. 看代码写结果

    class F1(object):
    def __init__(self, a1):
    self.a1 = a1

    def f2(self, arg):
    print(self.a1, arg)


    class F2(F1):
    def f2(self, arg):
    print('666')


    obj_list = [F1(1), F2(2), F2(3)]

    for obj in obj_list:
    obj.f2("喜羊羊")

    # F1(1) --> 1 喜羊羊
    # F2(2) --> 666
    # F3(3) --> 666


    # ---------------------------------------------------------------------------------#
    # 3.看代码写结果

    class F1(object):
    def __init__(self, a1):
    self.a1 = a1

    def f2(self, arg):
    print(self.a1, arg)


    class F2(F1):
    def f2(self, arg):
    print('666')


    obj_list = [F1(1), F2(2), F2(3)]

    for obj in obj_list:
    print(obj.f2("喜羊羊"))

    # F1(1) --> 1,喜羊羊
    # F2(2) --> 666
    # F2(3) --> 666

    # ---------------------------------------------------------------------------------#
    # 4.看代码写结果

    class F1(object):
    def __init__(self,num):
    self.num = num

    def func(self,request):
    print(self.num,request)

    def run(self):
    self.func(999)

    class F2(F1):
    def func(self,request):
    print(666,self.num)

    objs = [F1(1),F2(2),F2(3)]
    objs[1].run() #==> F2(2).run() --> 666 2
    objs[2].run() #==> F2(3).run() --> 666 3


    # ---------------------------------------------------------------------------------#
    # 5.看代码写结果

    class UserInfo(object):
    pass

    class Department(object):
    pass

    class StarkConfig(object):
    def __init__(self, num):
    self.num = num
    def changelist(self, request):
    print(self.num, request)
    def run(self):
    self.changelist(999)

    class RoleConfig(StarkConfig):
    def changelist(self, request):
    print(666, self.num)

    class AdminSite(object):

    def __init__(self):
    self._registry = {}

    def register(self, k, v): #UserInfo #StarkConfig
    self._registry[k] = v(k) #self._registry[UserInfo] = StarkConfig(UserInfo)

    site = AdminSite()
    site.register(UserInfo, StarkConfig) # self._registry = {UserInfo,StarkConfig}

    site.register(Department, RoleConfig) # self._registry = {Department,RoleConfig}

    for k, row in site._registry.items():
    row.run()

    # ---------------------------------------------------------------------------------#
    # 6.看代码写结果

    class UserInfo(object):
    pass

    class Department(object):
    pass

    class StarkConfig(object):
    def __init__(self, num):
    self.num = num

    def get_vals(self):
    v = [11, 22, 33]
    extra = self.extra_vals()
    if extra:
    v.extend(extra)
    return v
    def extra_vals(self):
    pass

    def run(self):
    return self.get_vals()

    class RoleConfig(StarkConfig):
    def extra_vals(self):
    return [99, 88]

    class AdminSite(object):
    def __init__(self):
    self._registry = {}
    def register(self, k, v):
    self._registry[k] = v(k)

    site = AdminSite()
    site.register(UserInfo, StarkConfig)
    site.register(Department, RoleConfig)
    for k, row in site._registry.items():
    print(row.run())

    # -- > 同第5题
    # ---------------------------------------------------------------------------------#

    # 7. 背写你了解的所有特殊方法并附示例;



    # ---------------------------------------------------------------------------------#

    # 8. 看代码写结果;

    class StarkConfig(object):
    def __init__(self, num):
    self.num = num
    def __call__(self, *args, **kwargs):
    print(self.num)

    class RoleConfig(StarkConfig):
    def __call__(self, *args, **kwargs):
    print(self.num)

    v1 = StarkConfig(1)
    v2 = RoleConfig(11)
    v1() #--> 1 , 11
    v2() # --> 11

    # ---------------------------------------------------------------------------------#

    # 9. 看代码写结果;

    class StarkConfig(object):
    def __init__(self, num):
    self.num = num

    def run(self):
    self()

    def __call__(self, *args, **kwargs):
    print(self.num)

    class RoleConfig(StarkConfig):

    def __call__(self, *args, **kwargs):
    print(345)

    v1 = StarkConfig(1)
    v2 = RoleConfig(11)
    print(v1.run()) # 1
    print(v2.run()) # 11 345



    # ---------------------------------------------------------------------------------#
    # 10. 看代码写结果。

    class StarkConfig(object):
    def __init__(self, num):
    self.num = num
    def run(self):
    self()
    def __call__(self, *args, **kwargs):
    print(self.num)

    class RoleConfig(StarkConfig):

    def __call__(self, *args, **kwargs):
    print(345)
    def __getitem__(self, item): #wupeiqi
    return self.num[item]

    v1 = RoleConfig('alex')
    v2 = StarkConfig("wupeiqi")
    print(v1[1]) #--> 345 alex
    print(v2[2]) #--> none 或者error



    # ---------------------------------------------------------------------------------#
    # 11. (升级题)
    '''
    1.完成学生选课系统
    学生选课
    学生:
    信息:学号,姓名,住址选的课程列表,
    功能:
    查看:查看该学生所有课程信息
    添加课程:把选好的课程添加到课程列表中(传参)
    课程:
    信息: 课程编号,课程名称,老师
    功能:
    查看:查看该课程的全部信息
    设置老师:给当前课程设置一个老师.(传参)
    老师:
    信息:老师编号,老师名称,老师电话
    功能:无

    请完成以上三个类.创建6个课程,6个老师.给课程安排好老师.然后创建30个学生
    每个学生随机被分配3个课程.最终显示出这三十个学生的选课情况以及任课老师的电话

    '''

    class student:
    def __init__(self,nom,name,adress):
    pass
    def skill(self,read,add_lesson):
    pass

    class lesson:
    def __init__(self,nom,leson_name,teacher):
    pass

    class Teacher:
    def __init__(self,nom,name,phone):
    pass

















































  • 相关阅读:
    面象对象设计原则之七:合成复用原则(Composition/Aggregate Reuse Principle, CARP)
    GRASP软件设计的模式和原则
    UniDAC 安装教程
    Delphi 实现检测线程类TThread是否结束
    DELPHI线程例子-FC
    Delphi Stringlist Delimiter如何区分TAB和空格
    DBGrid1
    UTF-8 delphi 函数
    未测试 Delphi读写UTF-8、Unicode格式文本文件
    mysql + unidac 使用事务例子
  • 原文地址:https://www.cnblogs.com/dealdwong2018/p/9932684.html
Copyright © 2011-2022 走看看