Python面向对象编程:派生

本套课在线学习视频(网盘地址,保存到网盘即可免费观看):

​https://pan.quark.cn/s/69d1cc25d4ba​

面向对象编程(OOP)是一种编程范式,它通过将数据和操作数据的方法封装在一起,形成对象,从而模拟现实世界的实体和它们之间的关系。继承是OOP中的一个核心概念,它允许通过创建新类(子类或派生类)基于现有类(父类或基类)来避免代码冗余,提高代码的重用性和可维护性。

00:00 - 面向对象编程:派生与继承

继承的基本概念

继承允许子类继承父类的属性和方法,从而避免重复定义相同的代码。子类可以自动获得父类的特征,并且可以添加或重写特定于子类的功能。

复制代码
class Person:
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex

    def speak(self):
        print(f"My name is {self.name}.")

class Student(Person):
    def __init__(self, name, age, sex, score, major):
        super().__init__(name, age, sex)
        self.score = score
        self.major = major

    def study(self):
        print(f"{self.name} is studying {self.major}.")

使用​​super​​关键字

​super​​关键字用于调用父类的方法,特别是在子类的初始化函数中,可以方便地继承父类的属性。

复制代码
class Student(Person):
    def __init__(self, name, age, sex, score, major):
        super().__init__(name, age, sex)
        self.score = score
        self.major = major

添加独特属性

在子类的初始化函数中,可以添加子类独有的属性,如​​score​​和​​major​​,以体现子类的独特性并增强其功能性。

复制代码
class Student(Person):
    def __init__(self, name, age, sex, score, major):
        super().__init__(name, age, sex)
        self.score = score
        self.major = major

01:46 - 创建继承自Person类的新类,并添加独特属性

定义新类

通过在新类中使用​​super​​关键字继承​​Person​​类的属性(如​​name​​、​​age​​、​​sex​​),并添加独有的属性(如​​score​​、​​major​​),从而实现新类的功能扩展与特化。

复制代码
class Student(Person):
    def __init__(self, name, age, sex, score, major):
        super().__init__(name, age, sex)
        self.score = score
        self.major = major

    def study(self):
        print(f"{self.name} is studying {self.major}.")

实例化新类

复制代码
student = Student("Alice", 20, "Female", 95, "Computer Science")
student.speak()  # Output: My name is Alice.
student.study()  # Output: Alice is studying Computer Science.

03:08 - 自定义属性与方法的实例化

定义自定义属性和方法

在类中定义自定义属性和方法,并通过实例化过程应用这些属性。使用​​self​​关键字访问实例变量,并通过​​super​​关键字继承父类属性。

复制代码
class Student(Person):
    def __init__(self, name, age, sex, score, major):
        super().__init__(name, age, sex)
        self.score = score
        self.major = major

    def study(self):
        print(f"{self.name} is studying {self.major}.")

实例化过程

复制代码
student = Student("Alice", 20, "Female", 95, "Computer Science")
student.speak()  # Output: My name is Alice.
student.study()  # Output: Alice is studying Computer Science.

构建子类并添加新属性

通过构建子类并添加新属性,可以满足特定需求,提升代码的复用性和可维护性。

复制代码
class GraduateStudent(Student):
    def __init__(self, name, age, sex, score, major, thesis):
        super().__init__(name, age, sex, score, major)
        self.thesis = thesis

    def research(self):
        print(f"{self.name} is researching on {self.thesis}.")

实例化子类

复制代码
graduate_student = GraduateStudent("Bob", 25, "Male", 90, "Artificial Intelligence", "Deep Learning")
graduate_student.speak()  # Output: My name is Bob.
graduate_student.study()  # Output: Bob is studying Artificial Intelligence.
graduate_student.research()  # Output: Bob is researching on Deep Learning.

总结

继承是面向对象编程中的一个重要概念,它通过创建新类基于现有类来避免代码冗余,提高代码的重用性和可维护性。通过将共有的属性和方法抽取到基类中,子类可以继承这些属性和方法,并且可以添加或重写特定于子类的功能。使用​​super​​关键字可以方便地调用父类的方法,特别是在子类的初始化函数中。通过构建子类并添加新属性,可以满足特定需求,提升代码的复用性和可维护性。

相关推荐
大圣编程2 小时前
Java 多维数组详解
java·开发语言
殳翰5 小时前
下服务器端开发流程及相关工具介绍(C++)
开发语言·c++
阿成学长_Cain5 小时前
Linux ipcs 命令超全详解:查看共享内存 / 消息队列 / 信号量,IPC 运维必备
linux·运维·服务器·网络·数据库
落寞的星星6 小时前
这个对象就包含了已经转换好的DFA和各种词法分析器运转所需要的参数。下一步,我们就可以用ScannerInfo对象创建出Scanner对象,请看下面的代码:
开发语言·c#
nothing&nowhere6 小时前
用 Python 做问卷数据清洗:无效样本检测与处理实战
开发语言·python·数据清洗·数据处理·问卷星·问卷星脚本·刷问卷
花酒锄作田6 小时前
如何发布自己的 Python 库到 PyPI
python
2601_961593426 小时前
Rust 开发环境配置繁琐?RustRover 开箱即用搞定编码调试
开发语言·后端·macos·rust
researcher-Jiang6 小时前
Design Patterns——Template Method入门到情景实战
python·设计模式·模板方法模式
HZZD_HZZD7 小时前
DL/T 645-2026新国标深度解读与智能电表协议适配实战:从帧结构变化到Java采集器升级的全链路改造方案
java·开发语言