python--重写

103 阅读1分钟

重写:

定义:

image.png

(一)代码如下:

#********** 8.7.3 重写 **********
# 定义一个表示人的类
class Person(object):
    def say_hello(self):
        print("打招呼!")

# 定义一个表示中国人的类
class Chinese(Person):
    def say_hello(self):  # 重写的方法
        print("吃了吗?")

chinese = Chinese()
chinese.say_hello()  # 子类调用重写的方法


#  定义一个表示人的类
class Person(object):
    def say_hello(self):
        print("打招呼!")
# 定义一个表示中国人的类
class Chinese(Person):
    def say_hello(self):
        super().say_hello()
        print("吃了吗?")
chinese = Chinese()

(二)运行结果如下:

image.png