首页 > 其他分享 >Pyhon的继承

Pyhon的继承

时间:2024-12-10 14:48:38浏览次数:3  
标签:__ 子类 name 继承 Pyhon self init Child

1 继承中的__init__初始化函数

1.1 子类没有定义自己的初始化函数,父类的初始化函数会被默认调用:

#定义父类:Parent
class Parent(object):
    def __init__(self, name):
        self.name = name
        print("create an instance of:", self.__class__.__name__)
        print("name attribute is:", self.name)
#定义子类Child ,继承父类Parent       
class Child(Parent):
    pass
#子类实例化时,由于子类没有初始化,此时父类的初始化函数就会默认被调用
#且必须传入父类的参数name
c = Child("init Child") 

# 输出
# create an instance of: Child
# name attribute is: init Child

子类实例化时,由于子类没有初始化,此时父类的初始化函数就会默认被调用
且必须传入父类的参数name,不传入会报错

1.2 子类定义了自己的初始化函数,而在子类中没有显示调用父类的初始化函数,则父类的属性不会被初始化

class Parent(object):
    def __init__(self, name):
        self.name = name
        print("create an instance of:", self.__class__.__name__)
        print("name attribute is:", self.name)
    
    def show(self):
        print("this is a method in Parent class")
        print("name attribute is:", self.name)

#子类继承父类        
class Child(Parent):
    #子类中没有显示调用父类的初始化函数
    def __init__(self):
        print("call __init__ from Child class")

# c = Child("init Child")  # 报错
#print()  
#将子类实例化  
c = Child()
# 输出 call __init__ from Child class
c.show()
# 报错,因为调用了 name 变量
# print(c.name)

这种情况下,子类重写了init初始化函数,可以调用父类的函数,但是父类中所有的变量都无效。

1.3 如果子类定义了自己的初始化函数,显示调用父类,子类和父类的属性都会被初始化

class Parent(object):
    def __init__(self, name):
        self.name = name
        print("create an instance of:", self.__class__.__name__)
        print("name attribute is:", self.name)

class Child(Parent):
    def __init__(self):
        print("call __init__ from Child class")
        super(Child,self).__init__("data from Child")   #要将子类Child和self传递进去
# c = Child("init Child") 
# print() 
# d = Parent('tom')   
c = Child()
print(c.name)

# 输出
# call __init__ from Child class
# create an instance of: Child
# name attribute is: data from Child
# data from Child

这里的super函数表示调用Child所继承的父类的初始化函数

References

标签:__,子类,name,继承,Pyhon,self,init,Child
From: https://www.cnblogs.com/kaopunotes/p/18597302

相关文章

  • 实验5继承与多态
    一.实验目的正确使用C++语法定义和使用派生类1.正确使用C++语法重载运算符,理解编译器是如何将表达式转换为对运算符重载函数的调用的2.基于问题场景,合理使用派生机制、虚函数、纯虚函数、抽象类,实现接口继承与运行时多态3.能灵活应用类的组合、继承、多态编程......
  • 实验5 继承和多态
    实验任务三源码如下:1#pragmaonce2#include<iostream>3#include<string>4usingstd::string;5usingstd::cout;6usingstd::endl;7classMachinePets{8private:9stringnickname;10public:11MachinePets(conststring......
  • 实验五 继承和多态
    任务3:#ifndefPETS_HPP#definePETS_HPP#include<string>#include<iostream>classMachinePets{protected:std::stringnickname;public:MachinePets(conststd::string&s):nickname(s){}virtualstd::stringtalk()=0......
  • 实验5 继承和多态
    实验任务3代码:pets.hpp1#pragmaonce2#include<iostream>34usingnamespacestd;56classMachinePets{7public:8MachinePets(conststring&s);9stringget_nickname()const;10virtualstringtalk()=0;1112privat......
  • 实验5 继承和多态
    任务1:task1.cpp1#include"publisher.hpp"2#include<vector>3#include<typeinfo>45usingstd::vector;67voidtest(){8vector<Publisher*>v;910v.push_back(newBook("HarryPotter","......
  • 实验5 继承和多态
    task3pets.hpp点击查看代码#pragmaonce#include<iostream>#include<string>usingstd::cout;usingstd::endl;usingstd::string;classMachinePets{public:MachinePets(conststring&s):nickname(s){}stringget_nickname()......
  • 实验5 继承和多态
    实验任务3:pets.hpp代码:1#pragmaonce2#include<iostream>3#include<string>4usingstd::string;56classMachinePets{7public:8//带参数的构造函数9MachinePets(conststring&s):nickname(s){}10//纯虚函数,......
  • 实验五 继承和多态
    task3pets.hpp#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststrings);virtualstringtalk()const=0;stringget_nickname();private:stringnickname;......
  • 实验5 继承和多态
    任务1:task1.cpp1publisher.hpp:2#pragmaonce3#include<iostream>4#include<string>56usingstd::cout;7usingstd::endl;8usingstd::string;910//发行/出版物类:Publisher(抽象类)11classPublisher{12public:13......
  • 实验5 继承和多态
    实验任务1:代码:publisher.hpp查看代码 #pragmaonce#include<iostream>#include<string>usingstd::cout;usingstd::endl;usingstd::string;//发行/出版物类:Publisher(抽象类)classPublisher{public:Publisher(conststring&s="");......