Python 类的继承

视频版教程 Python3零基础7天入门实战视频教程

继承是面向对象的三大特征之一,也是实现软件复用的重要手段。Python 的继承是多继承机制,即一个子类可以同时有多个直接父类。
语法:

class 子类(父类1,父类2,…):

子类继承父类的属性和方法,同时子类可以扩展新的属性和方法。

我们来看一个实例:

# 定义一个水果类,作为父类
class Fruit:
    # 父类属性 口感
    taste = None

    # 父类方法 打印口感
    def say_taste(self):
        print(f"口感:{self.taste}")


# 定义香蕉子类,继承父类Food
class Banana(Fruit):
    # 子类属性 颜色
    color = None

    def say_color(self):
        print(f"香蕉颜色:{self.color}")


b1 = Banana()
b1.taste = "果肉芳香"
b1.say_taste()
b1.color = "黄色"
b1.say_color()

运行输出:

口感:果肉芳香
香蕉颜色:黄色

多继承实例:

# 定义一个水果类,作为父类
class Fruit:
    # 父类属性 口感
    taste = None

    # 父类方法 打印口感
    def say_taste(self):
        print(f"口感:{self.taste}")


class Food:
    # 父类属性 价格
    price = None

    # 父类方法 打印价格
    def say_price(self):
        print(f"价格:{self.price}")


# 定义香蕉子类,继承父类Food
class Banana(Fruit, Food):
    # 子类属性 颜色
    color = None

    def say_color(self):
        print(f"香蕉颜色:{self.color}")

    def __str__(self):
        return f"口感:{self.taste},价格:{self.price},颜色:{self.color}"


b1 = Banana()
b1.taste = "果肉芳香"
b1.say_taste()
b1.color = "黄色"
b1.say_color()
b1.price = "10"
b1.say_price()
print(b1)

运行输出:

口感:果肉芳香
香蕉颜色:黄色
价格:10
口感:果肉芳香,价格:10,颜色:黄色

重写父类属性和方法

当子类根据业务需求,对父类方法不满意,我们可以在子类里重写父类方法,当然属性也可以重写。

# 定义一个水果类,作为父类
class Fruit:
    # 父类属性 口感
    taste = None

    # 父类方法 打印口感
    def say_taste(self):
        print(f"口感:{self.taste}")


class Food:
    # 父类属性 价格
    price = None

    # 父类方法 打印价格
    def say_price(self):
        print(f"价格:{self.price}")


# 定义香蕉子类,继承父类Food
class Banana(Fruit, Food):
    # 子类属性 颜色
    color = None

    # 重写父类属性
    price = 9

    def say_color(self):
        print(f"香蕉颜色:{self.color}")

    def __str__(self):
        return f"口感:{self.taste},价格:{self.price},颜色:{self.color}"

    # 重写父类方法 打印价格
    def say_price(self):
        print(f"香蕉价格:{self.price}")


b1 = Banana()
b1.taste = "果肉芳香"
b1.say_taste()
b1.color = "黄色"
b1.say_color()
print(b1.price)
b1.price = "10"
b1.say_price()
print(b1)

运行输出:

口感:果肉芳香
香蕉颜色:黄色
9
香蕉价格:10
口感:果肉芳香,价格:10,颜色:黄色

我们self调用的是子类继承下来的属性和方法。如果我们想调用父类的属性和方法,可以通过如下两种方式:

方式一:父类名.父类属性或者方法(self)

方式二:super().父类属性或者方法()

# 定义一个水果类,作为父类
class Fruit:
    # 父类属性 口感
    taste = None

    # 父类方法 打印口感
    def say_taste(self):
        print(f"口感:{self.taste}")


class Food:
    # 父类属性 价格
    price = 8

    # 父类方法 打印价格
    def say_price(self):
        print(f"价格:{self.price}")


# 定义香蕉子类,继承父类Food
class Banana(Fruit, Food):
    # 子类属性 颜色
    color = None

    # 重写父类属性
    price = 9

    def say_color(self):
        print(f"香蕉颜色:{self.color}")

    # 重写父类方法 打印价格
    def say_price(self):
        print(f"父类属性打印:{super().price},{Food.price}")
        # 父类方法调用
        super().say_price()
        Food.say_price(self)
        print(f"香蕉价格:{self.price}")


b1 = Banana()
b1.say_price()

运行效果:

父类属性打印:8,8
价格:9
价格:9
香蕉价格:9
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值