【Python面向对象】一个场景实现详解python面向对象语法

举例:学校把学生分为Junior和Senior管理,学校统一录入他们的姓名,年龄,都安排他们上课,实践,放假。上课内容:Junior学生主要接受通识教育,Senior学生上专业课。教师可以同时给这两类学生上课。对Junior学生成绩打分,对Senior学生论文写评语。

1. 构造器的继承

Junior和Senior继承Student类:
Student属性有:name,age
Juniort除继承属性外还有:score
Senior除继承属性外还有:essay
那各自的构造方法怎么实现呢:

class Student(object):
    def __init__(self,name,age):
        self.__name=name
        self.__age=age


class Junior(Student):
    def __init__(self,name,age,sore):
        super(Junior,self).__init__(name,age)
        self.sore=sore

class Senior(Student):
    def __init__(self,name,age,essay):
        super(Senior,self).__init__(name,age)
        self.essay=essay

要点:

  1. 调用基类的构造方法用super(子类,self).__init__(*attr1)。*attr1全部来自基类的位置参数;
  2. 定义子类的构造方法,参数列表按“self->继承的基类参数->自有的参数”排列:__init__(self,*attr1,*attr2)
  3. 不要忘了对自有参数*attr2初始化:self.attr2=attr2

2. getter、setter的继承

对于Student的属性name, age,不希望外部随意修改。所以要是用__*attr1这种方式来声明为私有属性,相应的,基类和子类要对应写getter和setter方法。

基类的getter和setter:

    def set_age(self,age):
        if 16<age<25:
            self.__age=age
        else:
            print('invalid age')
            
    def get_age(self):
        return self.__age

子类的getter和setter,要对Senior的年龄和Junior的年龄分段。

    def set_age(self,age):
    	if 16<age<21:
    		super(Junior,self).set_age(age)
    	else:
    		print('invalid age')

    def get_age(self):
        super(Junior,self).get_age()

要点:

  1. 子类重写getter和setter可以通过“super(子类,self)”直接调用基类的getter和setter
  2. 如果子类对继承的基类参数有更多的限制或处理操作,可以在getter和setter中进一步处理

3.多态的应用

教师要给学生上课,意味着可以给Junior学生上课,也可以给Senior学生上课。遵循开闭原则:
扩展开放:Junior类和Senior类通过继承,将Student类扩展。
修改开放:不需要修改学生上课的方法,通过子类的重写,实现基类方法在子类的不同动作。

定义Student类的方法:

    def lecture(self):
        print('Having classes')

Senior类重写:

    def lecture(self):
        print('Liberal Education')
        return self.sore

Junior类重写:

    def lecture(self):
        print('Professional Courses')
        return self.essay

创建教师类,初始化,定义上课方法:

class Teacher():

    def __init__(self,name):
        self.name=name

    def haveclass(self,student):
        print('give knowledge')
        student.lecture()

实例化所有对象,完成真正的调用:

selina_student=Junior('Selina',18,80)
adam_student=Senior('Adam',24,'title:******')

amy_teacher=Teacher('Amy')
amy_teacher.haveclass(selina_student)

john_teacher=Teacher('john')
john_teacher.haveclass(adam_student)

要点:

  1. 教师上课方法中,传入的位置参数是学生实例,并调用了学生的lecture()方法。这里的学生实例,可以是Student类的实例,也可以是两个子类的实例
  2. 所有对方法的操作必须通过对实例化对象的方法来调用。除非是静态方法或类方法

完整代码:

import sys
from functools import reduce


class Student(object):

    def __init__(self,name,age):
        self.__name=name
        self.__age=age

    def set_name(self,name):
        if not isinstance(name,str):
            print('invalid name')
        self.__name = name

    def get_name(self):
        return self.__name

    def set_age(self,age):
        if 16<age<25:
            self.__age=age
        else:
            print('invalid age')

    def get_age(self):
        return self.__age

    def lecture(self):
        print('Having classes')

    def fieldwork(self):
        print('Do fieldwork monthly')

    def holidy(self):
        print('No lecture!')

class Junior(Student):

    def __init__(self,name,age,sore):
        super(Junior,self).__init__(name,age)
        self.sore=sore

    def set_name(self,name):
        super(Junior,self).set_name(name)

    def get_name(self):
        super(Junior,self).get_name()

    def set_age(self,age):
        super(Junior,self).set_age(age)

    def get_age(self):
        super(Junior,self).get_age()

    def lecture(self):
        print('Liberal Education')
        return self.sore

    def fieldwork(self):
        print('Talk with staffs')

    def holidy(self):
        print('Play with friends')



class Senior(Student):

    def __init__(self,name,age,essay):
        super(Senior,self).__init__(name,age)
        self.essay=essay

    def set_name(self,name):
        super(Senior,self).set_name(name)

    def get_name(self):
        super(Senior,self).get_name()

    def set_age(self,age):
        super(Senior,self).set_age(age)

    def get_age(self):
        super(Senior,self).get_age()

    def lecture(self):
        print('Professional Courses')
        return self.essay

    def fieldwork(self):
        print('Conduct Experiments')

    def holidy(self):
        print('Travel around')

class Teacher():

    def __init__(self,name):
        self.name=name

    def haveclass(self,student):
        print('give knowledge')
        student.lecture()

    def evaluation_junior(self,junior):
        if junior.sore>=80:
            junior.get_name()
            print('    well done!')
        elif 70<junior.sore<80:
            junior.get_name()
            print('    make more effort')
        else:
            junior.get_name()
            print('    You have faild!')

    def evaluation_senior(self,senior):
        print("scan the essay")
        print(senior.essay+' '+'evaluation')

selina_student=Junior('Selina',18,80)
adam_student=Senior('Adam',24,'title:******')

amy_teacher=Teacher('Amy')
print('Teacher:Amy  '+'Junior Student:Selina')
amy_teacher.haveclass(selina_student)
amy_teacher.evaluation_junior(selina_student)

print('\n')

john_teacher=Teacher('john')
print('Teacher:John  '+'Junior Student:Adam')
john_teacher.haveclass(adam_student)
john_teacher.evaluation_senior(adam_student)

你可能感兴趣的:(Python学习)