zoukankan      html  css  js  c++  java
  • 类的组合,继承

    组合

     1 class School:
     2     def __init__(self,name,addr):
     3         self.name=name
     4         self.addr=addr
     5 
     6 
     7     def zhao_sheng(self):
     8         print('%s 正在招生' %self.name)
     9 
    10 class Course:
    11     def __init__(self,name,price,period,school):
    12         self.name=name
    13         self.price=price
    14         self.period=period
    15         self.school=school
    16 
    17 
    18 
    19 s1=School('oldboy','北京')
    20 s2=School('oldboy','南京')
    21 s3=School('oldboy','东京')
    22 
    23 # c1=Course('linux',10,'1h','oldboy 北京')
    24 # c1=Course('linux',10,'1h',s1)
    25 
    26 msg='''
    27 1 老男孩 北京校区
    28 2 老男孩 南京校区
    29 3 老男孩 东京校区
    30 '''
    31 while True:
    32     print(msg)
    33     menu={
    34         '1':s1,
    35         '2':s2,
    36         '3':s3
    37     }
    38     choice=input('选择学校>>: ')
    39     school_obj=menu[choice]
    40     name=input('课程名>>: ')
    41     price=input('课程费用>>: ')
    42     period=input('课程周期>>: ')
    43     new_course=Course(name,price,period,school_obj)
    44     print('课程【%s】属于【%s】学校' %(new_course.name,new_course.school.name))

     继承

    class Dad:
        '这个是爸爸类'
        money=10
        def __init__(self,name):
            print('爸爸')
            self.name=name
        def hit_son(self):
            print('%s 正在打儿子' %self.name)
    
    class Son(Dad):
        money = 1000000000009
        def __init__(self,name,age):
            self.name=name
            self.age=age
    
        def hit_son(self):
            print('来自儿子类')
    # print(Son.money)
    # Son.hit_son()
    # print(Dad.__dict__)
    # print(Son.__dict__)
    s1=Son('alex',18)
    s1.hit_son()
    # print(s1.money)
    # print(Dad.money)
    # print(s1.name)
    # print(s1.money)
    # print(s1.__dict__)
    # s1.hit_son()

     

     接口继承

    import abc
    class All_file(metaclass=abc.ABCMeta):
        @abc.abstractmethod
        def read(self):
            pass
    
        @abc.abstractmethod
        def write(self):
            pass
    
    class Disk(All_file):
        def read(self):
            print('disk read')
    
        def write(self):
            print('disk write')
    
    class Cdrom(All_file):
        def read(self):
            print('cdrom read')
    
        def write(self):
            print('cdrom write')
    
    
    class Mem(All_file):
        def read(self):
            print('mem read')
    
        def write(self):
            print('mem write')
    #
    m1=Mem()
    m1.read()
    m1.write()

     继承顺序

    #coding:utf-8
    class A:
        # def test(self):
        #     print('A')
        pass
    class B(A):
        # def test(self):
        #     print('B')
    
        pass
    class C(A):
        # def test(self):
        #     print('C')
        pass
    
    class D(B):
        # def test(self):
        #     print('D')
        pass
    
    class E(C):
        # def test(self):
        #     print('E')
        pass
    
    class F(D,E):
        # def test(self):
        #     print('F')
        pass
    f1=F()
    f1.test()   #经典类:F->D->B->A-->E-->
    
    # print(F.__mro__)
    
    #F-->D->B-->E--->C--->A新式类

     

  • 相关阅读:
    关于串通京东接口的demo
    http链接中请求进行编码,Http请求api
    RabbitMQ 创建用户和创建Virtual host
    dedecms调用当前栏目的子栏目及子栏目文章
    dede调出所有栏目以及栏目下的二级栏目
    JS判断移动端访问设备并加载对应CSS样式
    border-style
    网页色阶表
    破解centos7 密码
    DEDECMS打开网站后台系统首页卡解决方法
  • 原文地址:https://www.cnblogs.com/jiawen010/p/10075075.html
Copyright © 2011-2022 走看看