类的继承

类的继承

代码如下:

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_av(self):
        if self._age >= 18:
            print('%s正在观看动作片' % self._name)
        else:
            print('%s正在观看熊出没' % self._name)


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

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

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

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

    def chengji(self):
        print('%s的%s成绩为%d分' % (self._name, self._course, self._grade))


class Teacher(Person):
    def __init__(self, name, age, title, course):
        super().__init__(name, age)
        self._title = title
        self._course = course

    @property
    def title(self):
        return self._title

    @title.setter
    def title(self, title):
        self._title = title

    def teach(self):
        print('%s正在讲%s课' % (self._name, self._course))


def main():
    man1 = Student('小明', 8, 90, '思想品德')
    man2 = Teacher('张涛', 38, '教授', 'python课程设计')
    man1.study()
    man1.chengji()
    man2.teach()
    man1.play()
    man1.watch_av()
    man2.watch_av()


if __name__ == '__main__':
    main()

运行结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值