Python面向对象进阶

1.@property装饰器

类似java里的get, set方法。
复杂版:

class Student(object):

    def get_score(self):
         return self._score

    def set_score(self, value):
        if not isinstance(value, int):
            raise ValueError('score must be an integer!')
        if value < 0 or value > 100:
            raise ValueError('score must between 0 ~ 100!')
        self._score = value

使用@property装饰器版:

class Student(object):

    @property
    def score(self):
        return self._score

    @score.setter
    def score(self, value):
        if not isinstance(value, int):
            raise ValueError('score must be an integer!')
        if value < 0 or value > 100:
            raise ValueError('score must between 0 ~ 100!')
        self._score = value

2.继承

class Person(object):
    def __init__(self, name, age):
        self._name = name
        self._age = age

    @property
    def name(self):
        return self._name

    @property
    def age(self):
        return self._age

    @age.setter
    def age(self, age):
        self._age = age

    def play(self):
        print('%s正在愉快的玩耍' % self._name)

    def watch_movies(self):
        if self._age >= 18:
            print('%s正在观看《一拳超人》.' % self._name)
        else:
            print('%s只能观看《喜洋洋》.' % self._name)

class Student(Person):
    def __init__(self, name, age, grade):
        super().__init__(name, age)
        self._grade = grade

    @property
    def grade(self):
        return self._grade

    @grade.setter
    def grade(self, grade):
        self._grade = grade

    def study(self, course):
        print('%s的%s正在学习%s.' % (self._grade, self._name, course))

def main():
    stu = Student('郭德纲', 42, '初三')
    stu.study('数学')
    stu.watch_movies()
if __name__ == '__main__':
    main()

3.重载

from abc import ABCMeta, abstractclassmethod

class Employee(object, metaclass=ABCMeta):
    def __init__(self, name):
        self._name = name

    @property
    def name(self):
        return self._name

    @abstractclassmethod
    def get_salary(self):
        pass

class Manager(Employee):
    # 想一想: 如果不定义构造方法会怎么样
    def __init__(self, name):
        # 想一想: 如果不调用父类构造器会怎么样
        super().__init__(name)

    def get_salary(self):
        return 15000.0

class Programmer(Employee):
    def __init__(self, name, working_hour):
        super().__init__(name)
        self._working_hour = working_hour

    @property
    def working_hour(self):
        return self._working_hour

    @working_hour.setter
    def working_hour(self, working_hour):
        self._working_hour = working_hour if working_hour > 0 else 0

    def get_salary(self):
        return 150.0 * self._working_hour

class Salesman(Employee):
    def __init__(self, name, sales):
        super().__init__(name)
        self._sales = sales

    @property
    def sales(self):
        return self._sales

    @sales.setter
    def sales(self):
        self._sales = sales if sales > 0 else 0

    def get_salary(self):
        return 1200.0 + self._sales * 0.05

def main():
    emps = [
        Manager('刘备'), Programmer('诸葛亮', 150), Salesman('荀彧', 10000)
    ]
    for emp in emps:
        print('%s本月工资为: ¥%s元' % (emp.name, emp.get_salary()))


if __name__ == '__main__':
    main()

你可能感兴趣的:(Python)