Python 继承

1. Python继承

继承表示允许我们继承另一个类的所有方法和属性的类。

父类是继承的类,称基类。

子类是从另一个类继承类,称派生类。

 

2. 创建父类

任何类都可以是父类。

实例

创建一个 Person 类,其中包含 firstname lastname 属性以及 printname 方法。

复制代码
class Person():
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

x = Person('Bill', 'Gates')
x.printname()
复制代码

Bill Gates

 

3. 创建子类

创建子类时将父类作为参数发送:

实例

创建一个 Student 类,将从 Person 类继承属性和方法。

class Student(Person):
    pass

现在,Student 类与 Person 类有相同的属性和方法。

注释:不想在类中使用其他属性或方法,使用 pass 关键字。

复制代码
class Person():
   def __init__(self, fname, lname):
      self.firstname = fname
      self.lastname = lname

   def printname(self):
      print(self.firstname, self.lastname)

class Student(Person):
   pass

# 使用 Student 类创建一个对象,执行 printname方法
x = Student('wang', 'ke')
x.printname()
复制代码

wang ke

 

4. 添加 __init__() 函数

我们已经创建一个子类,它继承了父类的属性和方法。

下面我们把  __init__() 函数添加到子类。

注释:每次使用类创建新对象,都会调用 __init__() 函数

实例

Student 类添加 __init__() 函数:

class Student(Person):
    def __init__(self, fname, lname):
        # 添加属性

当添加 __init__() 函数时,子类将不再继承父的 __init__() 函数

注释:子类的 __init__() 函数会覆盖父类的 __init__() 函数。如需保持父类的 __init__() 函数,添加对父类 __init__() 函数的调用。

实例

复制代码
class Person():
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

class Student(Person):
    def __init__(self, fname, lname):
        Person.__init__(self, fname, lname)

x = Student('wang', 'ke')
x.printname()
x = Person('Bill', 'Gates')
x.printname()
复制代码

wang ke
Bill Gates

 

已经成功添加 __init__() 函数,并保留父类的继承,接下来在 __init__() 函数中添加功能。

 

5. supper() 函数

supper() 函数使子类从父类继承所有方法和属性:

复制代码
class Person():
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

class Student(Person):
    def __init__(self, fname, lname):
        super().__init__(fname, lname)

x = Student('wang', 'ke')
x.printname()
复制代码

wang ke

 

使用 supper() 函数,不必使用父元素的名称,它将自动从父元素继承方法和属性。

 

6. 添加属性

把名为 graduationyear 的属性添加到 Student 类:

复制代码
class Person():
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

class Student(Person):
    def __init__(self, fname, lname):
        super().__init__(fname, lname)
        self.graduationyear = 2019

x = Student('wang', 'ke')
print(x.graduationyear)
复制代码

2019

 

上例中,2019是一个变量,在创建 student 对象时传递到 Student 类。为此,在 __init__() 函数中添加另一个参数:

复制代码
class Person():
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

class Student(Person):
    def __init__(self, fname, lname, year):
        super().__init__(fname, lname)
        self.graduationyear = year

x = Student("wang", "ke", 2019)
print(x.graduationyear)
复制代码

 2019

 

7. 添加方法

welcome 方法添加到 Student 类:

复制代码
class Person():
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

class Student(Person):
    def __init__(self, fname, lname, year):
        super().__init__(fname, lname)
        self.graduationyear = year

    def welcome(self):
        print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)
x = Student("wang", "ke", 2019)
x.welcome()
复制代码

Welcome wang ke to the class of 2019

 

提示:如果在子类中添加一个与父类中的函数同名的方法,将覆盖父类方法的继承

 

posted @   做梦当财神  阅读(745)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2020-07-22 特征选择(2)
2018-07-22 清华镜像方法更新python包
点击右上角即可分享
微信分享提示