📚 Object-Oriented Programming: Core Concepts | 面向对象编程核心概念
Object-oriented programming (OOP) is a paradigm that organises software around objects containing data and methods, promoting modularity and reusability. It models real-world entities and their interactions, forming the foundation of many modern languages like Java, C++, and Python. This article explains the core OOP concepts as required for A-Level Edexcel Computer Science, including classes, objects, encapsulation, inheritance, polymorphism, and more.
面向对象编程(OOP)是一种根据包含数据和方法的对象来组织软件的范式,能提升模块化和可复用性。它对现实世界实体及其交互进行建模,是 Java、C++、Python 等现代语言的基石。本文讲解 A-Level Edexcel 计算机科学所需的 OOP 核心概念,包括类、对象、封装、继承、多态等。
1. Programming Paradigms | 编程范式概述
Programming paradigms are fundamental styles of programming that provide a way of thinking about code structure. The two main paradigms are procedural and object-oriented. In procedural programming, the program is split into procedures or functions; in OOP, it is split into objects. OOP models real-world entities more naturally.
编程范式是编程的基本风格,提供了一种思考代码结构的方式。两大范式是过程式与面向对象。过程式编程将程序划分为过程或函数;而 OOP 则划分为对象。OOP 能更自然地模拟现实世界实体。
2. Classes and Objects | 类与对象
A class is a blueprint or template that defines the attributes (data) and methods (behaviour) common to all objects of a certain kind. An object is an instance of a class; it holds specific values for the attributes and can execute the defined methods. For example, a class Car might have attributes make, model, speed and methods accelerate(), brake(). An object myCar would have actual values like ‘Toyota’, ‘Corolla’, 0.
类是定义某一类对象共有属性(数据)和方法(行为)的蓝图或模板。对象是类的实例,持有属性的具体值,并能执行定义的方法。例如,类 Car 可能有属性 make、model、speed 和方法 accelerate()、brake()。对象 myCar 则具有实际值,如 ‘Toyota’、’Corolla’、0。
3. Encapsulation and Data Hiding | 封装与数据隐藏
Encapsulation bundles attributes and methods inside a class and restricts direct access to some of an object’s components. This is often implemented by declaring attributes as private and providing public getter and setter methods. It protects data integrity and reduces coupling between modules.
封装将属性和方法捆绑在类内部,并限制对对象某些组件的直接访问。通常通过将属性声明为私有、并公开 getter 和 setter 方法来实现。它能保护数据完整性并降低模块间的耦合度。
4. Inheritance | 继承
Inheritance allows a new class (subclass) to acquire the properties and methods of an existing class (superclass). This promotes code reuse and establishes a hierarchical relationship. For example, a SportsCar subclass could inherit from Car and add a turboBoost() method. Edexcel often tests single inheritance and the ‘is-a’ relationship.
继承允许新类(子类)获取已有类(超类)的属性和方法。这促进了代码复用并建立层次关系。例如,SportsCar 子类可继承自 Car,并添加 turboBoost() 方法。Edexcel 常考单继承和 “is-a” 关系。
5. Polymorphism | 多态
Polymorphism means ‘many forms’. In OOP, it allows objects of different classes to be treated as objects of a common superclass. The most common form is method overriding, where a subclass provides a specific implementation of a method already defined in its superclass. This enables dynamic method dispatch at runtime.
多态意为“多种形态”。在 OOP 中,它允许将不同类的对象当作共同超类的对象来处理。最常见的形式是方法重写,即子类为其超类中已定义的方法提供特定实现。这使得在运行时能进行动态方法分派。
6. Abstract Classes and Interfaces | 抽象类与接口
An abstract class cannot be instantiated; it serves as a base class that defines a common interface for subclasses. It may contain both abstract methods (without implementation) and concrete methods. An interface is a contract that lists method signatures without any implementation. Classes implement interfaces to guarantee certain behaviours. Java and C# distinguish between abstract classes and interfaces, while C++ uses pure virtual functions.
抽象类不能实例化;它作为基类为子类定义公共接口,可包含抽象方法(无实现)和具体方法。接口是一种契约,仅列出方法签名而不提供实现。类通过实现接口来保证特定行为。Java 和 C# 区分抽象类与接口,C++ 使用纯虚函数。
7. Association, Aggregation, and Composition | 关联、聚合与组合
Objects can be related through association, a general connection between classes. Aggregation is a ‘has-a’ relationship where one class contains a reference to another, but the contained object can exist independently (e.g., a library has books). Composition is a stronger ‘part-of’ relationship where the contained object’s lifecycle depends on the container (e.g., a house is composed of rooms).
对象可通过关联(类之间的一般连接)相互联系。聚合是一种“has-a”关系,一个类包含对另一个类的引用,但被包含对象可独立存在(例如图书馆有书)。组合是更强的“part-of”关系,被包含对象的生命周期依赖容器(例如房子由房间组成)。
8. Method Overloading | 方法重载
Method overloading is a form of compile-time polymorphism where multiple methods have the same name but different parameter lists (number, types, or order). This improves code readability and allows similar operations to be performed with different inputs. For instance, add(int a, int b) and add(double a, double b).
方法重载是编译时多态的一种形式,即多个方法同名但参数列表不同(数量、类型或顺序)。这提高了代码可读性,允许用不同输入执行相似操作。例如 add(int a, int b) 和 add(double a, double b)。
9. Access Modifiers | 访问修饰符
Access modifiers control the visibility of class members. Common modifiers are public (accessible everywhere), private (only within the same class), and protected (accessible within the package and subclasses). These enforce encapsulation and safeguard sensitive data.
访问修饰符控制类成员的可见性。常见的有 public(全局可访问)、private(仅在同一类内)和 protected(包内及子类可访问)。它们强制封装并保护敏感数据。
10. Constructors and Destructors | 构造函数与析构函数
A constructor is a special method that initialises a new object. It typically has the same name as the class and no return type. Overloaded constructors allow different initialisation scenarios. A destructor (or garbage collector in some languages) cleans up when an object is destroyed.
构造函数是初始化新对象的特殊方法,通常与类同名、无返回类型。重载构造函数支持不同的初始化方式。析构函数(或某些语言中的垃圾收集器)在对象销毁时进行清理。
11. OOP Design Principles (SOLID) | 面向对象设计原则 (SOLID)
While not always tested explicitly, understanding SOLID principles can deepen OOP knowledge. SOLID stands for: Single responsibility, Open/closed, Liskov substitution, Interface segregation, and Dependency inversion. These guide developers to create maintainable, scalable systems.
尽管不常直接考查,理解 SOLID 原则能加深 OOP 认识。SOLID 代表:单一职责、开闭原则、里氏替换、接口隔离和依赖反转。它们指导开发者构建可维护、可扩展的系统。
12. Advantages and Disadvantages of OOP | 面向对象的优缺点
Advantages include modularity, reusability, easier maintenance, and natural modelling. Disadvantages may be increased complexity for small programs, steeper learning curves, and potential performance overhead due to indirection. Edexcel expects candidates to evaluate these trade-offs.
优点包括模块化、可复用性、易维护和自然建模。缺点可能是对小程序增加复杂度、学习曲线较陡、以及由间接调用造成的潜在性能开销。Edexcel 期望考生能评估这些取舍。
Published by TutorHao | Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导