编程学习 /Python /实战应用 /面向对象编程入门
🐍 Python 实战应用

面向对象编程入门

理解类与对象,用封装组织真实业务模型。

📅 2026-11-23 👁 1 次阅读 🔗 参考 Python 官方文档 docs.python.org
面向对象编程入门示意图

类与对象

class Student:
    def __init__(self, name, score):
        self.name = name
        self.score = score

    def introduce(self):
        return f"我是{self.name},考了{self.score}分"

s = Student("小明", 92)
print(s.introduce())

__init__ 是构造方法,self 代表实例本身。定义在类里的函数叫方法,通过对象调用。

类与对象关系示意图

继承

class AdvancedStudent(Student):
    def honor(self):
        return "优秀学生" if self.score >= 90 else "继续加油"

子类继承父类的属性和方法,并可重写(override)以满足自己的需要。