首页 > 编程语言 >Python重用父类方法

Python重用父类方法

时间:2023-01-02 16:12:33浏览次数:45  
标签:__ name Python self 重用 父类 super age def

一、重用父类方法

1 与继承没有关系的重用

  • 指名道姓的使用

  • 在子类里想用父类的方法,我们可以直接用父类名.方法名()--->父类里方法有几个参数就传几个参数

  • 我们看起来是子类在调用父类的方法,但是实际上,这并没有存在继承关系

class A:
    def __init__(self,name,age):
        self.name=name
        self.age=age
        
class Person:
    school = 'oldboy'
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def study(self):
        print('study....')

class Teacher(Person):
    def __init__(self,name,age,level):
        A.__init__(self,name,age)
        self.level=level

class Student(Person):
    school = 'yyyy'
    def __init__(self,name,age,course):
        Person.__init__(self,name,age)
        self.course=course
    def study(self):
        Person.study(self)
        print("%s学生在学习"%self.name

2 与继承有关系的重用

super关键字

super在经典类和新式类使用的区别

经典类

  • super(Student,self).__init__(name,age)----->Student:当前子类,self:当前对象
  • python3中没有经典类,所以这种方式一般是在python2中写
  • python3中也会有这种写法,这样写是为了代码可以向下兼容,拿到python2中也可以直接使用

新式类

  • super().__init__(name,age):括号里面不加参数
  • super() 会按照__mro__列表拿到父类对象
  • 它是通过对象来调用绑定方法的,不需要传递第一个参数,对象调用父类的绑定方法时,对自动将对象自己传进去
#Python交流学习Q群:711312441
class Person(object):
    school = 'oldboy'
    def __init__(self,name,age):
        self.name=name
        self.age=age
    def study(self):
        print('study....')

class Student(Person):
    school = 'yyyy'
    def __init__(self,name,age,course):
        #super() 会按照mro列表拿到父类对象
        super().__init__(name,age)
        # super(Student,self).__init__(name,age)
        self.course=course
    def study(self):
        Person.study(self)
        super().study()
        print("%s学生在学习"%self.name)


stu1=Student('wed',19,"Python")
stu1.study()
print(Student.__mro__)
study....

study....

wed学生在学习
(<class 'main.Student'>, <class 'main.Person'>, <class 'object'>)

二、重用父类两种方法的使用

1 指名道姓的使用

类名.类里的方法

  • 一般在没有继承关系的时候使用
  • 如果继承了多个父类,super是按照mro列表找,现在想确切的用某个父类的某个方法,就需要指名道姓的使用

2 super的使用

  • super().方法名()
  • 有继承关系的时候使用
  • super()相当于得到了一个特殊对象,第一个参数不需要传,调用绑定方法,会把自己传过去
  • 使用super方法,它用父类的方法时,按照对象的mro列表的顺序来查找
class A:
    def f1(self):
        print('A.f1')
class B:
    def f1(self):
        print('B.f1')
    def f2(self):
        print('B.f2')
        super().f1()
        # return 'xxxxx'

#class C(A,B):
#注意这个顺序,这个顺序报错
class C(B,A):
    def f1(self):
        print('C.f1')
        
        
        
c=C()
c.f2()
print(C.mro())
B.f2
A.f1

[<class 'main.C'>, <class 'main.B'>, <class 'main.A'>, <class 'object'>]

标签:__,name,Python,self,重用,父类,super,age,def
From: https://www.cnblogs.com/djdjdj123/p/17020028.html

相关文章

  • Linux下更新Python版本
    参考:安装图形化配置解析工具_LiteOS_编译和开发工具_Linux下的编译_搭建Linux编译环境_华为云(huaweicloud.com)系统:Centos7$uname-aLinuxlocalhost.localdom......
  • Python类的封装教程
    一、什么是封装封装的本身意思其实就和闭包函数一样,就是把一个函数和变量全都包在一起,但其实这样的说法不是很具体,就是一种很片面的解释二、为什么要封装封装数据的主要......
  • python调用sklearn库实现svr拟合数据
    参考的是b站南方小鱼儿的代码,参考网址:SVR模型对连续量的预测(SVM)02-生成样本数据_哔哩哔哩_bilibili代码如下importnumpyasnpfromsklearn.svmimportSVRimpor......
  • /home/software/python/Modules/_ctypes/_ctypes.c:118:17: fatal error: ffi.h: No s
     001、python3.11编译报错/home/software/python/Modules/_ctypes/_ctypes.c:118:17:fatalerror:ffi.h:Nosuchfileordirectory  002、解决方法[root@PC......
  • vscode设置python代码debug(调试) 与带参数调试
    VScode官方调试说明:https://code.visualstudio.com/docs/python/debugging#_set-configuration-options 一、带参数的Debug调试,launch.json文件创建来源1.新建p......
  • GitHub 上 25 个 Python 学习资源,墙裂推荐!
    “阅读本文大概需要7分钟。”英文:thecarrots根据2020年StackOverflow开发者调查报告,Python是世界上最受欢迎的语言之一,排名仅次于Rust和TypeScript。更令人惊讶的......
  • Python - 情景管理器
    defmy_function():logging.debug('Somedebugdata')logging.error('Errorloghere')logging.debug('Moredebugdata')有这样一个函数,它的默认日志级......
  • 极客编程python入门-多进程
    多进程在Python程序中轻松创建子进程:importosprint('Process(%s)start...'%os.getpid())#OnlyworksonUnix/Linux/Mac:pid=os.fork()ifpid==0:print('Iam......
  • python中如何获取主机的ip和主机名
    使用python中的socket库,可以轻松获取主机ip和主机名。一、获取主机名 importsockethostname=socket.gethostname()print(hostname)#DESKTOP-AAI12V0 ......
  • python中的platform模块
     platform模块给我们提供了很多方法去获取操作系统的信息;importplatformprint(platform.platform())#获取操作系统名称和版本号:macOS-10.14.6-x86_64-i386-64bitpri......