03python面向对象编程4

定义一个Person类:

class Person:
    '这是一个学习Python定义的一个Person类'
    # 下面定义了一个类变量
    hair = 'black'

    def __init__(self, name='Charlie', age=8):
        # 下面为Person对象增加2个实例变量
        self.name = name
        self.age = age

    # 下面定义了一个say方法
    def say(self, content):
        print(content)

 

class Dog:
    # 定义一个jump()方法
    def jump(self):
        print("正在执行jump方法")

    # 定义一个run()方法,run()方法需要借助jump()方法
    def run(self):
        # 使用self参数引用调用run()方法的对象
        self.jump()
        print("正在执行run方法")

class Dog:
    # 定义一个jump()方法
    def jump(self):
        print("正在执行jump方法")

    # 定义一个run()方法,run()方法需要借助jump()方法
    def run(self):
        # 报错
        jump()
        print("正在执行run方法")

猜你喜欢

转载自www.cnblogs.com/xinmomoyan/p/10807166.html