Object-Oriented Programming: Classes, Objects and Inheritance | 面向对象编程:类、对象与继承

📚 Object-Oriented Programming: Classes, Objects and Inheritance | 面向对象编程:类、对象与继承

Object-oriented programming (OOP) is a core topic in the Edexcel A-Level Programming unit. Understanding classes, objects, inheritance and related concepts is essential for both exam questions and practical programming tasks.

面向对象编程(OOP)是 Edexcel A-Level 编程单元的核心主题。理解类、对象、继承及相关概念对考试题和实际编程任务都至关重要。


1. Introduction to Programming Paradigms | 编程范式概述

A programming paradigm is a fundamental style of writing code. Edexcel candidates should be able to compare procedural programming, object-oriented programming, and event-driven programming. In OOP, code is organised around objects that combine data and behaviour, whereas procedural programming separates data and functions.

编程范式是编写代码的基本风格。Edexcel 考生应能比较过程式编程、面向对象编程和事件驱动编程。在面向对象编程中,代码围绕结合数据与行为的对象组织,而过程式编程将数据与函数分离。


2. Classes and Objects | 类与对象

A class is a template that defines the common attributes and methods of a category of objects. An object is a concrete instance created from that template. For example, a class called Student might have attributes such as name and age, and methods such as enrol().

类是定义某类对象共同属性和方法的模板。对象是根据该模板创建的具体实例。例如,名为 Student 的类可能具有 name 和 age 等属性,以及 enrol() 等方法。

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

    def enrol(self):
        return self.name + " is enrolled"

s1 = Student("Ali", 17)
print(s1.enrol())

The code above defines a Student class and creates an object s1. The constructor assigns values to instance attributes, and the method returns a message.

上面的代码定义了一个 Student 类并创建了对象 s1。构造函数为实例属性赋值,方法返回一条消息。


3. Attributes and Methods | 属性与方法

Attributes represent the state of an object; methods represent its behaviour. Instance attributes are stored separately for each object, while class attributes are shared by all instances. In Edexcel pseudocode, attributes are often shown in class definitions with their data types.

属性表示对象的状态;方法表示其行为。实例属性为每个对象单独存储,而类属性由所有实例共享。在 Edexcel 伪代码中,属性通常在类定义中与其数据类型一起显示。


4. Encapsulation | 封装

Encapsulation hides an object’s internal data and only exposes necessary methods. This protects data integrity and reduces unintended interference. In Python, a leading underscore indicates a protected member, and a double underscore activates name mangling for a private-like member.

封装隐藏对象的内部数据,只公开必要的方法。这保护了数据完整性并减少意外干扰。在 Python 中,前导单下划线表示受保护成员,双下划线会触发名称改写,形成类似私有成员。


5. Constructors and Instantiation | 构造函数与实例化

A constructor is a special method that initialises a new object. In Python, the constructor is named __init__. The parameter self refers to the current instance being created. Instantiation is the process of allocating memory and calling the constructor.

构造函数是初始化新对象的特殊方法。在 Python 中,构造函数名为 __init__。参数 self 指向正在创建的当前实例。实例化是分配内存并调用构造函数的过程。


6. Inheritance | 继承

Inheritance allows one class to acquire the attributes and methods of another class. The existing class is the parent or superclass; the new class is the child or subclass. The child can add new members or modify inherited members.

继承允许一个类获得另一个类的属性和方法。已有的类是父类或超类;新类是子类。子类可以添加新成员或修改继承的成员。

class Shape:
    def area(self):
        return 0

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

Here Rectangle inherits from Shape and overrides the area method to compute the correct result.

此处 Rectangle 继承自 Shape,并重写 area 方法以计算正确结果。


7. Polymorphism and Method Overriding | 多态与方法重写

Polymorphism lets a single method name behave differently depending on the object that calls it. Method overriding is the key mechanism: a subclass provides its own version of a method defined in the superclass. This supports flexibility and cleaner code.

多态允许同一个方法名根据调用它的对象表现出不同行为。方法重写是关键机制:子类为超类中定义的方法提供自己的版本。这支持了灵活性和更简洁的代码。


8. Association, Aggregation and Composition | 关联、聚合与组合

Class relationships are important in object-oriented design. Association is a general relationship such as a teacher and a student. Aggregation is a ‘has-a’ relationship in which the part can exist independently, such as a department and a teacher. Composition is stronger: the part cannot exist without the whole, such as a house and a room.

类之间的关系在面向对象设计中很重要。关联是一般关系,如教师和学生。聚合是 ‘has-a’ 关系,其中部分可以独立存在,如部门和教师。组合更强:部分不能脱离整体存在,如房子和房间。


9. Advantages and Disadvantages of OOP | 面向对象的优点与缺点

Advantages include improved modularity, code reuse through inheritance, easier maintenance and better modelling of real-world systems. Disadvantages include a steeper learning curve, potential performance overhead, and unnecessary complexity for very small programs.

优点包括改进的模块化、通过继承实现代码重用、更容易维护以及更好地对现实系统建模。缺点包括学习曲线更陡、潜在的性能开销,以及对非常小的程序可能带来不必要的复杂性。


10. Design and Exam-Style Questions | 设计与考试题型

Edexcel exam questions often ask you to identify classes, attributes, methods and relationships from a scenario, or to outline the advantages of OOP. You should practise drawing simple UML class diagrams and converting them into code.

Edexcel 考试题目通常要求你从场景中识别类、属性、方法和关系,或概述面向对象的优点。你应练习绘制简单的 UML 类图并将其转换为代码。

  • Read the scenario carefully and underline nouns for potential classes.
  • Underline verbs for potential methods.
  • Check inheritance relationships for ‘is-a’ and aggregation for ‘has-a’.

阅读场景并仔细标出名词作为潜在的类;标出动词作为潜在的方法;检查 ‘is-a’ 继承关系和 ‘has-a’ 聚合关系。


11. Common Mistakes and Exam Tips | 常见错误与考试提示

Many candidates confuse a class with an object. Remember: the class is the blueprint, and the object is the instance. Another common mistake is forgetting to use the constructor correctly or misunderstanding the difference between aggregation and composition.

许多考生混淆类和对象。记住:类是蓝图,对象是实例。另一个常见错误是未能正确使用构造函数,或误解聚合与组合的区别。

Tip: when writing inheritance, always ask whether the relationship is truly ‘is-a’. If not, use association or composition instead of inheritance.

提示:在编写继承时,始终问自己关系是否真的是 ‘is-a’。如果不是,请使用关联或组合而不是继承。


Published by TutorHao | Programming Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading