• python学习之面向对象property、继承与菱形问题作业


    作业一:自己试验一下菱形问题下的属性查找顺序

    #    经典类:深度优先,会在检索第一条分支的时候就直接一条道走到黑,即会检索大脑袋(共同的父类)
    
    #    新式类:广度优先,会在检索最后一条分支的时候检索大脑袋
    
    #在python3中所有类都是新式类,无经典类、新式类之分;
    #在python2中类分为经典类和新式类
    #以下代码是在python2中运行的代码
    class G: # 在python2中,未继承object的类及其子类,都是经典类
        def test(self):
            print('from G')
        pass
    
    class E(G):
        def test(self):
            print('from E')
        pass
    
    class F(G):
        def test(self):
            print('from F')
    
    class B(E):
        def test(self):
            print('from B')
        pass
    
    class C(F):
        def test(self):
            print('from C')
    
    class D(G):
        def test(self):
            print('from D')
    
    class A(B,C,D):
        def test(self):
            print('from A')
        pass
    
    # 新式类
    
    # print(A.mro()) # A->B->E->C->F->D->G->object
    
    # 经典类:在python2中没有继承object类及其子子孙孙类都是经典类
    
    # print(A.mro()) # A->B->E->G->C->F->D
    
    obj = A()
    obj.test()
    
    # 总结:类相关的属性查找(类名.属性,该类的对象.属性),都是参照该类的mro
    

    作业二:在昨天作业的基础之上

    1、引入属性访问控制+property

    2、引入继承与派生的概念来减少代码冗余

    注意:要满足什么"是"什么的关系,不满足"是"的关系不要去继承

    class School():
    
    ​    school_name = 'Oldboy'
    ​    def __init__(self,nick_name,address):
    ​        self.nick_name = nick_name
    ​        self.address = address
    ​        self.classes = []
    
    ​    def related_class(self,class_obj):
    ​        self.classes.append(class_obj)
    
    ​    def tell_info(self):
    ​        print('='*22+' %s '%self.nick_name+'='*22)
    ​        for class_obj in self.classes:
    ​            class_obj.tell_info()
    
    class Class():
    
    ​    def __init__(self,name):
    ​        self.name = name
    ​        self.course = None
    
    ​    def related_course(self,course_obj):
    ​        self.course = course_obj
    
    ​    def tell_info(self):
    ​        print('%s     '%self.name,end='')
    ​        self.course.tell_info()
    
    class Course():
    
    ​    def __init__(self,name,period,price):
    ​        self.name = name
    ​        self.period = period
    ​        self.price = price
    
    ​    def tell_info(self):
    ​        print('<课程名:%s 周期:%s 价格:%s>'%(self.name,self.period,self.price))
    
    
    
    class Student():
    
    ​    def __init__(self,name,age,sex):
    ​        self.name = name
    ​        self.age = age
    ​        self.sex = sex
    ​        self.course = []
    ​        self.class_name = None
    
    ​    def related_course(self,course_name):
    ​        self.course.append(course_name)
    ​    def related_class(self,class_name):
    ​        self.class_name = class_name
    
    ​    def tell_info(self):
    ​        print('='*20+' 学生信息 '+'='*20)
    ​        print('姓名:%s 年龄:%s 性别:%s 班级:%s'%(
    ​            self.name,self.age,self.sex,self.class_name
    ​        ))
    ​        print('课程信息:')
    ​        for course in self.course:
    ​            course.tell_info()
    
    cour1 = Course('python开发','6 months','20000')
    cour2 = Course('linux运维','6 months','18000')
    
    class1 = Class('python14期')
    class1.related_course(cour1)
    class2 = Class('python 9期')
    class2.related_course(cour1)
    class3 = Class('linux 25期')
    class3.related_course(cour2)
    
    sch1 = School('老男孩魔都校区','上海')
    sch1.related_class(class1)
    sch1.related_class(class3)
    sch2 = School('老男孩帝都校区','北京')
    sch2.related_class(class2)
    
    stu1 = Student('vincent','18','male')
    stu1.related_class(class1.name)
    stu1.related_course(cour1)
    stu1.related_course(cour2)
    stu1.tell_info()
    
    
    
  • 相关阅读:
    懒人模式Singleton模式Meyers版本号
    欢迎CSDN-markdown编辑
    协同编辑多人word一个小技巧文件
    72_leetcode_Construct Binary Tree from Preorder and Inorder Traversal
    2015第44周五Java集群技术(转)
    linux远程管理工具
    2015第44周三提升个人价值意识
    2015第44周二
    2015第44周一
    2015第43周日
  • 原文地址:https://www.cnblogs.com/leilijian/p/12670636.html
Copyright © 2020-2023  润新知