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

相关推荐
南境十里·墨染春水4 小时前
C++传记(面向对象)虚析构函数 纯虚函数 抽象类 final、override关键字
开发语言·c++·笔记·算法
无巧不成书02184 小时前
30分钟入门Java:从历史到Hello World的小白指南
java·开发语言
2301_797172754 小时前
基于C++的游戏引擎开发
开发语言·c++·算法
成为大佬先秃头5 小时前
数据库连接池:Druid
数据库·mysql·druid
比昨天多敲两行5 小时前
C++ 二叉搜索树
开发语言·c++·算法
Birdy_x6 小时前
接口自动化项目实战(1):requests请求封装
开发语言·前端·python
我爱学习好爱好爱6 小时前
Ansible 常用模块详解:lineinfile、replace、get_url实战
linux·python·ansible
海海不瞌睡(捏捏王子)6 小时前
C++ 知识点概要
开发语言·c++
桌面运维家7 小时前
VLAN配置进阶:抑制广播风暴,提升网络效率
开发语言·网络·php
一轮弯弯的明月7 小时前
Python基础-速通秘籍(下)
开发语言·笔记·python·学习