十三. python面向对象 组合执行顺序

一 .python面向对象 组合执行顺序(空间调用 ,不多说直接看代码理解)

 

# 类的命名空间调用
class Grad(object):
    bar=555555
    def __init__(self,num):
          self.num=num
    def aa(self):
        print("相当的99999999999999999S")

class Son(Grad):
    def aa(self):
        print("相当的6666666666666666666")
ret=[Grad(1),Grad(2),Son(3)]
for itme in  ret:
    print(itme.num)   #  1 2 3
    itme.aa()
# 执行顺序
# 相当的99999999999999999S
# 相当的99999999999999999S
# 相当的6666666666666666666"""

print("*********************************************")

class Grad(object):
        bar = 555555
        def __init__(self, num):
            self.num = num

        def aa(self):
            print("相当88888888888888888888888")

class Son(Grad):
      def __init__(self,num):
          self.num=num
      def bb(self):
          print(self.num,"啦啦啦啦啦啦啦啦啦啦")
ret = [Son(55), Grad(2), Son(6)]
for itme in ret:
    print(itme.num)
    itme.aa()
# 执行顺序
# 55
# 相当88888888888888888888888
# 2
# 相当88888888888888888888888
# 6
# 相当88888888888888888888888

print("*********************************************")

class Grad(object):
        bar = 555555
        def __init__(self, num):
            self.num = num
        def aa(self):
            print("相当88888888888888888888888")
        def run(self):
            self.aa()
class Son(Grad):
    def __init__(self, num):
        self.num = num

    def bb(self):
        print(self.num, "啦啦啦啦啦啦啦啦啦啦")
ret = [Son(55), Grad(2), Son(6)]
for itme in ret:
    print(itme.num)
    itme.run()
# 执行顺序
# 55
# 相当88888888888888888888888
# 2
# 相当88888888888888888888888
# 6
# 相当88888888888888888888888

print("*********************************************")

class Grad(object):
        bar = 555555
        def __init__(self, num):
            self.num = num
        def aa(self,er):
            print("相当哈哈哈哈哈哈哈哈哈---",er)
        def run(self):
            self.aa(999)

class Son(Grad):
    def aa(self,er):
        print(self.num, "啦啦啦啦啦啦啦啦啦啦---",er)
ret = [Son(55), Grad(2), Son(6)]

ret[0].aa(888)
ret[1].run()
ret[2].aa(5555)

# 执行顺序
# 55 啦啦啦啦啦啦啦啦啦啦--- 888
# 相当哈哈哈哈哈哈哈哈哈--- 999
# 6 啦啦啦啦啦啦啦啦啦啦--- 5555

 

 二. 类嵌套组合

# 学校  地址   校区
class School(object):
      def __init__(self,addr,cam):
          self.addr=addr
          self.cam=cam
a1=School("北京","沙河校区")
a2=School("深圳","罗湖校区")
a3=School("上海","浦东校区")


class Teacher(object):
      def __init__(self,name,age,money,school=None):
              self.name=name
              self.age=age
              self.__money=money
              self.school=school
          
b1=Teacher("王老师",25,"3000元",a1)
b2=Teacher("苍老师",25,"3555元",a2)
b3=Teacher("李老师",25,"8555元",a3)

class Grade(object):
    def __init__(self,name,itme,mubers,teacher=None):
        self.name=name
        self.itme=itme
        self.mubers=mubers
        self.teacher=teacher
c=Grade("前端3期","2008年","6个月",b3)

print(c.name)
print(c.itme)

print(c.teacher.name)
print(c.teacher.school.cam)
print(c.teacher.school.addr)
class School(object):
    def __init__(self,name,addr,less):
        self.name=name
        self.addr=addr
        self.less=less
    def aa(self):
        print("学校有课程了哈哈哈%s"%self.less)
obj1=School("新东方","北京校区","python")
obj2=School("新东方","上海校区","linux")
obj3=School("新东方","深圳校区","java")


class Techar(object):
    def __init__(self,name,age,money):
        self.name=name
        self.age=age
        self.__money=money
        self.schol=None
    @property
    def bb(self):
       return self.__money

a1=Techar("王老师","25",9222)
a2=Techar("李老师","15",522)
a3=Techar("仓老师","19",6333)

a1.schol=obj1
print(f'把{a1.name}--安排到{a1.schol.name},--{a1.schol.addr}---上{a1.schol.less},而工资是--{a1.bb}元')
a2.schol=obj2
print(f'把{a2.name}--安排到{a2.schol.name},--{a2.schol.addr}---上{a2.schol.less},而工资是--{a2.bb}元')
a3.schol=obj3
print(f'把{a3.name}--安排到{a3.schol.name},--{a3.schol.addr}---上{a3.schol.less},而工资是--{a3.bb}元')
class School(object):
    def __init__(self,name,addr,less):
        self.name=name
        self.addr=addr
        self.less=less
    def aa(self):
        print("学校有课程了哈哈哈%s"%self.less)
obj1=School("新东方","北京校区","python")
obj2=School("新东方","上海校区","linux")
obj3=School("新东方","深圳校区","java")


class Techar(object):
    def __init__(self,name,age,money):
        self.name=name
        self.age=age
        self.__money=money
        self.schol=School
    @property
    def bb(self):
       return self.__money

a1=Techar("王老师","25",9222)
a2=Techar("李老师","15",522)
a3=Techar("仓老师","19",6333)

a1.schol=obj1
print(f'把{a1.name}--安排到{a1.schol.name},--{a1.schol.addr}---上{a1.schol.less},而工资是--{a1.bb}元')
a2.schol=obj2
print(f'把{a2.name}--安排到{a2.schol.name},--{a2.schol.addr}---上{a2.schol.less},而工资是--{a2.bb}元')
a3.schol=obj3
print(f'把{a3.name}--安排到{a3.schol.name},--{a3.schol.addr}---上{a3.schol.less},而工资是--{a3.bb}元')

 

三 .分页实现

class Page_number(object):
     def __init__(self,pagr,ret,pag_nu=10):
            self.pagr=pagr
            self.ret=ret
            self.pag_nu=pag_nu
     @property
     def start(self):
         return  (self.pagr-1)*self.pag_nu       #   起始位置

     @property
     def ende(self):
       return self.pagr*self.pag_nu   #  结束位置

     @property
     def show(self):
          for roe in  self.ret[obj.start:obj.ende]:
              print(roe)

ret=[]
for i in  range(1,99):
    ret.append("lover%s"%i)
while True:
    page = int(input("请输入页码!!!!!"))
    obj=Page_number(page,ret)
    obj.show
ret=[]
for i in  range(1,99):
    ret.append("lover%s"%i)

while True:
    page=int(input("请输入页码!!!!!"))
    page_num=10
    star=(page-1)*10
    end=page*10
    for row in ret[star:end]:
          print(row)
class Page_number(object):
    def __init__(self, pagr, pag_nu=10):
        self.pagr = pagr
      
        self.pag_nu = pag_nu

    @property
    def start(self):
        return (self.pagr - 1) * self.pag_nu  # 起始位置
    @property
    def ende(self):
        return self.pagr * self.pag_nu  # 结束位置
ret = []
for i in range(1, 99):
    ret.append("lover%s" % i)
while True:
    page = int(input("请输入页码!!!!!"))
    obj = Page_number(page)
    for row in  ret[obj.start:obj.ende]:
         print(row)

 

转载于:https://www.cnblogs.com/Sup-to/p/11062315.html

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页