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

相关推荐
星星点点洲24 分钟前
【缓存与数据库结合最终方案】伪从技术
数据库·缓存
小黑屋的黑小子27 分钟前
【MySQL】MySQL索引与事务
数据库·mysql·oracle
XIE39227 分钟前
Browser-use使用教程
python
酷爱码1 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
画个大饼1 小时前
Go语言实战:快速搭建完整的用户认证系统
开发语言·后端·golang
蹦蹦跳跳真可爱5891 小时前
Python----深度学习(基于深度学习Pytroch簇分类,圆环分类,月牙分类)
人工智能·pytorch·python·深度学习·分类
喵先生!2 小时前
C++中的vector和list的区别与适用场景
开发语言·c++
OK_boom3 小时前
Dapper的数据库操作备忘
数据库
Thomas_YXQ3 小时前
Unity3D Lua集成技术指南
java·开发语言·驱动开发·junit·全文检索·lua·unity3d
xMathematics3 小时前
计算机图形学实践:结合Qt和OpenGL实现绘制彩色三角形
开发语言·c++·qt·计算机图形学·cmake·opengl