栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 教学

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 教学

第二章 数据结构、函数、类、系统内部库 第四节 类的继承

上节课我们学习了如何定义一个类,使用类,这节课我们来学习类的继承吧。

类的继承,就像是一个妈妈和一个孩子,孩子拥有一些妈妈的特性,还有一些自己新的特性。

那我们先来学习类是如何继承的吧!

import os
class cat():
    def __init__(self,name,age,weight):
        self.__name = name
        self.__age = age
        self.__weight = weight
    def introduce(self):
        print("小猫咪%s%d岁了,它的体重是%d千克。"%(self.__name,self.__age,self.__weight))
    def say(self):
        os.system("say 喵喵喵~喵喵喵~喵喵喵")
    def revise(self,name,age,weight):
        self.__name = name
        self.__age = age
        self.__weight = weight
    def __plus(self,a,b):
        return a + b
    def __minus(self,a,b):
        return a - b
    def __cheng(self,a,b):
        return a * b
    def __chu(self,a,b):
        return a / b
    def coculater(self,a,b,c):
        if c == "+":
            print(self.__plus(a,b))
        elif c == "-":
            print(self.__minus(a,b))
        elif c == "*":
            print(self.__cheng(a,b))
        else:
            print(self.__chu(a,b))

class kitten(cat):
    pass

在新定义的kitten类里写上它的父类,就可以完成类的继承了。

小猫咪跟大猫咪的叫声不一样,是喵呜喵呜叫的,我们就应该在kitten类中定义一个say函数来覆盖父类cat里的say函数。

import os
class cat():
    def __init__(self,name,age,weight):
        self.__name = name
        self.__age = age
        self.__weight = weight
    def introduce(self):
        print("小猫咪%s%d岁了,它的体重是%d千克。"%(self.__name,self.__age,self.__weight))
    def say(self):
        os.system("say 喵喵喵~喵喵喵~喵喵喵")
    def revise(self,name,age,weight):
        self.__name = name
        self.__age = age
        self.__weight = weight
    def __plus(self,a,b):
        return a + b
    def __minus(self,a,b):
        return a - b
    def __cheng(self,a,b):
        return a * b
    def __chu(self,a,b):
        return a / b
    def coculater(self,a,b,c):
        if c == "+":
            print(self.__plus(a,b))
        elif c == "-":
            print(self.__minus(a,b))
        elif c == "*":
            print(self.__cheng(a,b))
        else:
            print(self.__chu(a,b))

class kitten(cat):
    def say(self):
        os.system("say 喵呜~喵呜~喵呜")
    

现在,我想定义一个新的introduce函数来介绍它的姓名、年龄、体重。

介绍格式如下:

可爱的小猫咪喵喵5岁了,它重10千克。

那这样,cat类里的私有变量就必须改成公有变量了。

import os
class cat():
    def __init__(self,name,age,weight):
        self.name = name
        self.age = age
        self.weight = weight
    def introduce(self):
        print("小猫咪%s%d岁了,它的体重是%d千克。"%(self.name,self.age,self.weight))
    def say(self):
        os.system("say 喵喵喵~喵喵喵~喵喵喵")
    def revise(self,name,age,weight):
        self.name = name
        self.age = age
        self.weight = weight
    def __plus(self,a,b):
        return a + b
    def __minus(self,a,b):
        return a - b
    def __cheng(self,a,b):
        return a * b
    def __chu(self,a,b):
        return a / b
    def coculater(self,a,b,c):
        if c == "+":
            print(self.__plus(a,b))
        elif c == "-":
            print(self.__minus(a,b))
        elif c == "*":
            print(self.__cheng(a,b))
        else:
            print(self.__chu(a,b))

class kitten(cat):
    def say(self):
        os.system("say 喵呜~喵呜~喵呜")
    def introduce(self):
        print("可爱的小猫咪%s%d岁了,它重%d千克。"%(self.name,self.age,self.weight))

还有一种情况,就是一个类继承了多个类。

比如说下面这样的。

import os
class cat():
    def __init__(self,name,age,weight):
        self.name = name
        self.age = age
        self.weight = weight
    def introduce(self):
        print("小猫咪%s%d岁了,它的体重是%d千克。"%(self.name,self.age,self.weight))
    def say(self):
        os.system("say 喵喵喵~喵喵喵~喵喵喵")
    def revise(self,name,age,weight):
        self.name = name
        self.age = age
        self.weight = weight
    def __plus(self,a,b):
        return a + b
    def __minus(self,a,b):
        return a - b
    def __cheng(self,a,b):
        return a * b
    def __chu(self,a,b):
        return a / b
    def coculater(self,a,b,c):
        if c == "+":
            print(self.__plus(a,b))
        elif c == "-":
            print(self.__minus(a,b))
        elif c == "*":
            print(self.__cheng(a,b))
        else:
            print(self.__chu(a,b))

class animal():
    def __init__(self,a,name,age,weight):
        self.animal = a
        self.name = b
        self.age = age
        self.weight = weight
    def say_animal(self):
        os.system("say 我是一个可爱的动物")
        
class kitten(cat,animal):
    def say(self):
        os.system("say 喵呜~喵呜~喵呜")
    def introduce(self):
        print("可爱的小猫咪%s%d岁了,它重%d千克。"%(self.name,self.age,self.weight))

最后,我们来讲一下多次的继承。

import os
class animal():
    def __init__(self,name,age,weight):
        self.name = name
        self.age = age
        self.weight = weight
    def introduce(self):
        print("小动物%s%d岁了,它的体重是%d千克。"%(self.name,self.age,self.weight))
    def say(self):
        os.system("say 我是一个可爱的动物")
    def revise(self,name,age,weight):
        self.name = name
        self.age = age
        self.weight = weight
    def __plus(self,a,b):
        return a + b
    def __minus(self,a,b):
        return a - b
    def __cheng(self,a,b):
        return a * b
    def __chu(self,a,b):
        return a / b
    def coculater(self,a,b,c):
        if c == "+":
            print(self.__plus(a,b))
        elif c == "-":
            print(self.__minus(a,b))
        elif c == "*":
            print(self.__cheng(a,b))
        else:
            print(self.__chu(a,b))
        
class cat(animal):
    def introduce(self):
        print("小猫咪%s%d岁了,它的体重是%d千克。"%(self.name,self.age,self.weight))
    def say(self):
        os.system("say 喵喵喵~喵喵喵~喵喵喵")
        
class kitten(cat):
    def say_hello(self):
        os.system("say 我好可爱啊")
    def introduce(self):
        print("可爱的小猫咪%s%d岁了,它重%d千克。"%(self.name,self.age,self.weight))

这节课我们就讲到这里,下节课我们来讲字典。

下次更新时间6月4日或6月5日。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/949689.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号