Python class and object

Posted ipyanthony

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python class and object相关的知识,希望对你有一定的参考价值。

# Python继承
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):
        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))


class Teacher(Person):
    """老师"""

    def __init__(self, name, age, title):
        super().__init__(name, age)
        self._title = title

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

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

    def teach(self, course):
        print(%s%s正在讲%s. % (self._name, self._title, course))


def main():
    stu = Student(王二小, 15, 初三)
    stu.study(数学)
    stu.watch_av()
    t = Teacher(‘Anthony, 38, 老叫兽)
    t.teach(Python程序设计)
    t.watch_av()


if __name__ == __main__:
    main()

 

以上是关于Python class and object的主要内容,如果未能解决你的问题,请参考以下文章

Python class and object

Python OOP-static method,class method and instance method

class13and14and15_登录窗口

016: class, objects and instance: method

python3_list and tuple

[Python] Find available methods and help in REPL