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​​关键字可以方便地调用父类的方法,特别是在子类的初始化函数中。通过构建子类并添加新属性,可以满足特定需求,提升代码的复用性和可维护性。

相关推荐
ClickHouseDB3 小时前
ClickHouse Cloud 与 Snowflake 实时性价比对比:CostBench 测试中的 412 倍差距来
运维·数据库·系统架构
第九序言T3 小时前
Python 数据处理进阶(十八)批量处理同结构文件:一个文件夹一次跑完
开发语言·python·自动化·pandas·数据处理
m0_639906943 小时前
Redis入门|五种数据结构、使用场景、持久化RDB&AOF
数据结构·数据库·redis
Java后端的Ai之路3 小时前
CAS并发控制-从CPU指令到企业级乐观锁
vue.js·人工智能·python·cas·乐观锁
qq_426003963 小时前
UI自动化高频次登录login限流解决方案
python·ui·自动化
Sakura_j_h_3 小时前
EasyEDA PCB 快速设计Skills
c++·python·github
时间售卖铺4 小时前
触发器与函数实战:MySQL高效编程秘诀
运维·数据库·mysql·云原生
qetfw4 小时前
UOS MariaDB 数据库:安装、最小权限与备份验证
linux·数据库·mariadb·uos
龙亘川4 小时前
构建城市公共安全数字底座:亘川智城智慧安防系统全维度解读
java·大数据·开发语言·人工智能·信息可视化
程序喵大人4 小时前
【C++入门】内存模型与资源管理 - 09 shared_ptr 和 weak_ptr 解决共享,也带来代价
开发语言·c++·资源管理·智能指针·内存模型