📚 A-Level AQA Computer Science: Object-Oriented Programming Key Concepts | A-Level AQA 计算机科学:面向对象编程考点精讲
Object-oriented programming (OOP) is a paradigm centred on objects rather than actions. It forms a core part of the AQA A-Level Computer Science specification, requiring you to understand how classes, encapsulation, inheritance and polymorphism work together to create maintainable and reusable code. This article breaks down every key concept you need to master for the exam.
面向对象编程是一种以对象而非动作为中心的编程范式。它是AQA A-Level 计算机科学考试大纲的核心内容,要求你理解类、封装、继承和多态如何协同工作,以构建可维护、可重用的代码。本文将分解你需要掌握的每一个关键概念。
1. Introduction to Object-Oriented Programming | 面向对象编程简介
Object-oriented programming organises software design around data, or objects, rather than functions and logic. An object combines state (fields) and behaviour (methods) into a single entity, modelling real-world items more naturally than procedural code.
面向对象编程围绕数据(即对象)而非功能与逻辑来组织软件设计。一个对象将状态(字段)与行为(方法)组合成一个单一实体,比面向过程的代码更自然地模拟现实世界中的事物。
The core aim of OOP is to increase modularity, reduce complexity, and promote code reuse. By encapsulating related data and the operations that manipulate that data, programs become easier to debug, test, and extend.
面向对象编程的核心目标是提高模块化、降低复杂性并促进代码重用。通过封装相关数据及操作这些数据的方法,程序更容易调试、测试和扩展。
2. Classes and Objects | 类与对象
A class is a blueprint or template that defines the attributes and methods common to all objects of a certain kind. It specifies what data an object will hold and what operations it can perform, but does not contain actual data values itself.
类是一个蓝图或模板,定义了某一类对象共有的属性和方法。它规定了对象将持有何种数据以及可以执行哪些操作,但类本身不包含实际数据的值。
An object is an instance of a class. When you create (instantiate) an object, memory is allocated and the attributes are initialised with specific values. Multiple objects can be created from the same class, each with its own unique state.
对象是类的实例。当你创建(实例化)一个对象时,系统会分配内存,属性被赋予具体数值。可以从同一个类创建多个对象,每个对象都拥有自己独特的状态。
// Example in Java-like pseudocode
class Car {
String model;
int year;
void start() { ... }
}
Car myCar = new Car(); // 'myCar' is an object of class Car
3. Encapsulation and Data Hiding | 封装与数据隐藏
Encapsulation bundles the data (attributes) and the methods that operate on that data into a single unit, the class. It prevents external code from directly accessing an object’s internal representation, enforcing controlled interaction through well-defined interfaces.
封装将数据(属性)和操作数据的方法捆绑在同一个单元——类中。它阻止外部代码直接访问对象的内部表示,强制通过定义良好的接口进行受控交互。
Data hiding is a key consequence of encapsulation. Attributes are typically declared as private, and public getter and setter methods are provided to read or modify their values. This allows validation and maintenance without altering the external interface.
数据隐藏是封装的一个关键结果。属性通常被声明为私有的(private),并提供公共的getter和setter方法来读取或修改其值。这样就可以在不改变外部接口的情况下进行验证和维护。
For example, if an ‘age’ attribute must be positive, the setter method can enforce that rule, preventing invalid states that would be possible with direct public access.
例如,如果一个“年龄”属性必须为正数,setter方法可以强制执行该规则,从而防止直接公开访问可能导致的无效状态。
4. Inheritance | 继承
Inheritance allows a new class (subclass or derived class) to absorb the properties and methods of an existing class (superclass or base class). The subclass can reuse, extend, or modify the behaviour inherited from the parent, modelling an “is-a” relationship.
继承允许一个新类(子类或派生类)吸收现有类(父类或基类)的属性和方法。子类可以重用、扩展或修改从父类继承而来的行为,从而建立一种“是”(is-a)的关系。
This mechanism drastically reduces code duplication. Common functionality is defined once in the superclass, and all subclasses automatically gain it. For instance, a ‘Dog’ subclass of ‘Animal’ inherits a ‘breathe()’ method without needing to redefine it.
这种机制大幅减少了代码重复。共同的功能在父类中定义一次,所有子类自动获得。例如,’Animal’的’Dog’子类继承了’breathe()’方法,无需重新定义。
In AQA pseudocode and Java, the keyword ‘extends’ or the colon ‘:’ (in pseudocode) indicates inheritance. A subclass can add its own unique attributes and methods, or override inherited ones to provide specialised behaviour.
在AQA的伪代码和Java中,关键字’extends’或者冒号’:’(伪代码中)表示继承关系。子类可以添加自己独有的属性和方法,或重写继承的方法以实现专门化行为。
5. Polymorphism | 多态
Polymorphism means “many forms”. It allows objects of different classes to be treated as objects of a common superclass, while each class responds to the same method call in its own specific way. The exact method executed is determined at runtime by the actual object’s type.
多态意为“多种形态”。它允许不同类的对象被当作共同的父类对象来对待,而每个类以自己的特定方式响应相同的方法调用。具体执行哪个方法由运行时对象的实际类型决定。
This is typically achieved through method overriding. When a superclass reference points to a subclass object, calling an overridden method invokes the subclass version. This is dynamic (late) binding and enables flexible, extensible designs.
多态通常通过方法重写来实现。当父类引用指向子类对象时,调用重写的方法会调用子类的版本。这就是动态(晚期)绑定,使设计灵活且可扩展。
For instance, a list of ‘Shape’ references could hold ‘Circle’ and ‘Rectangle’ objects, and calling the ‘draw()’ method on each would produce the appropriate graphical output without conditional statements.
例如,一个’S hape’引用列表可以容纳’Circle’和’Rectangle’对象,对每个对象调用’draw()’方法将产生相应的图形输出,而无需条件判断语句。
6. Constructors | 构造方法
A constructor is a special method within a class that is automatically invoked when a new object is instantiated. Its primary role is to initialise the object’s attributes to valid states. In most languages, the constructor has the same name as the class.
构造方法是类中的特殊方法,在实例化新对象时自动调用。其主要作用是初始化对象的属性至合法状态。在大部分语言中,构造方法与类同名。
You can overload constructors to provide multiple ways of creating an object. A constructor that takes parameters allows initialisation with user-supplied values, while a default (no-argument) constructor often sets fields to default values.
你可以重载构造方法,以提供多种创建对象的方式。带参数的构造方法允许用用户提供的值初始化,而默认(无参)构造方法通常将字段设为默认值。
class Book {
String title;
// default constructor
Book() { title = "Untitled"; }
// parameterised constructor
Book(String t) { title = t; }
}
Note that if a class explicitly defines any constructor, the compiler will not provide an automatic default constructor, unless you manually include one.
注意,如果类显式定义了任何构造方法,编译器将不再提供自动的默认构造方法,除非你手动包含一个。
7. Method Overloading and Overriding | 方法重载与重写
Overloading occurs when multiple methods in the same class share the same name but have different parameter lists (number, type, or order of parameters). It is an example of compile-time (static) polymorphism because the appropriate method is resolved at compile time.
重载发生在同一个类中出现多个同名方法,但参数列表不同(参数的数量、类型或顺序)。这是编译时(静态)多态的一个例子,因为合适的方法在编译时就被确定了。
Overriding, on the other hand, allows a subclass to provide a specific implementation of a method that is already defined in its superclass. The method signature must be identical. This enables runtime (dynamic) polymorphism, where the subclass version is called via a superclass reference.
另一方面,重写允许子类为已在父类中定义的方法提供特定实现。方法签名必须完全相同。这实现了运行时(动态)多态,即通过父类引用调用的是子类版本。
A common mistake is confusing the two. Overloading deals with methods inside one class, while overriding involves a superclass–subclass relationship. The @Override annotation in Java is used by the compiler to check you are correctly overriding a method.
一个常见的错误是混淆两者。重载处理的是同一个类内部的方法,而重写涉及父类与子类的关系。Java中的@Override注解由编译器用来检查你是否正确地重写了方法。
8. Access Modifiers | 访问修饰符
Access modifiers control the visibility of class members (attributes and methods) to other parts of the program. The main modifiers in AQA-relevant languages are private, public, and protected. They are fundamental to enforcing encapsulation.
访问修饰符控制类成员(属性和方法)对程序其他部分的可见性。在AQA相关语言中,主要的修饰符有private、public和protected。它们是贯彻封装的基础。
| Modifier | Class | Package/Subclass | World |
|---|---|---|---|
| private | ✓ | ✗ | ✗ |
| (default) | ✓ | ✓ | ✗ |
| protected | ✓ | ✓ | ✗ |
| public | ✓ | ✓ | ✓ |
Private members are only accessible within the same class, forming the strongest barrier. Public members can be accessed from any other class. Protected sits in between, allowing access within the same package and by subclasses, even if they are in different packages.
私有成员只能在同一类内部访问,形成了最强的屏障。公共成员可以从任何其他类访问。受保护(protected)介于两者之间,允许在同一个包内以及子类中访问,即使它们位于不同的包中。
9. Abstract Classes and Interfaces | 抽象类与接口
An abstract class is a class that cannot be instantiated on its own and may contain abstract methods——methods without a body that must be implemented by any concrete subclass. It can also contain concrete methods and instance variables.
抽象类是无法自行实例化的类,它可以包含抽象方法(即没有方法体的方法),这些方法必须由任何具体的子类实现。抽象类也可以包含具体方法和实例变量。
An interface defines a contract of method signatures that implementing classes must provide. In many languages such as Java, a class can implement multiple interfaces, enabling a form of multiple inheritance of type. Interfaces usually contain only abstract method declarations and constants, though modern Java allows default methods.
接口定义了一个方法签名契约,实现类必须提供这些方法。在许多语言(如Java)中,一个类可以实现多个接口,从而实现一种类型的多重继承。接口通常只包含抽象方法声明和常量,尽管现代Java允许默认方法。
The key difference: abstract classes can hold state (instance variables) and potentially complete method implementations, while interfaces focus purely on “what” an object can do, not “how”. In exam questions, you must be able to choose the appropriate abstraction tool.
关键区别:抽象类可以持有状态(实例变量)以及可能完整的方法实现,而接口纯粹专注于对象“能做什么”,而不是“如何做”。在考试题目中,你必须能够选择合适的抽象工具。
10. Association, Aggregation, and Composition | 关联、聚合与组合
These describe relationships between objects. Association is a broad term for a “uses-a” relationship, where one object interacts with another. Aggregation and composition are specialised forms of association that represent “has-a” relationships.
这些术语描述对象之间的关系。关联是一个广义的术语,表示“使用”(uses-a)关系,其中一个对象与另一个对象交互。聚合与组合是关联的特殊形式,表示“拥有”(has-a)的关系。
Aggregation is a weak “whole–part” relationship where the part can exist independently of the whole. For example, a department has employees, but employees can exist even if the department is dissolved. In UML, it is shown with an empty diamond at the whole end.
聚合是一种弱的“整体-部分”关系,其中部分可以独立于整体而存在。例如,一个部门拥有雇员,但即便部门解散,雇员依然可以存在。在UML中,整体一端用空心菱形表示。
Composition is a strong relationship where the part cannot exist without the whole. The whole is responsible for the lifecycle of its parts. For instance, a house is composed of rooms; if the house is destroyed, the rooms cease to exist. UML uses a filled diamond.
组合是一种强关系,其中部分不能脱离整体而存在。整体负责其组成部分的生命周期。例如,一所房子由房间组成;如果房子被摧毁,房间也就不复存在。UML使用实心菱形表示。
11. Advantages and Disadvantages of OOP | 面向对象的优缺点
Advantages: OOP promotes modularity through well-defined classes, making large projects easier to manage. Code reusability via inheritance speeds up development. Encapsulation improves security and maintainability. Polymorphism simplifies code by allowing uniform interfaces.
优点:面向对象编程通过定义良好的类提升了模块化,使大型项目更易于管理。通过继承实现代码重用加快了开发速度。封装提高了安全性和可维护性。多态通过允许统一接口简化了代码。
Disadvantages: OOP can introduce complexity, especially when inheritance hierarchies become deep and tangled. Efficient object-oriented programs may be slower and consume more memory than equivalent procedural programs due to the overhead of dynamic dispatch and object instantiation.
缺点:面向对象编程可能会引入复杂性,特别是当继承层次结构变得深而纠结时。由于动态派发和对象实例化的开销,高效的面向对象程序可能比等效的过程式程序运行得更慢且消耗更多内存。
Additionally, designing a good class hierarchy requires thorough analysis and foresight. Poor design can lead to rigid code that is difficult to refactor. For small, simple tasks, OOP can feel overly ceremonious.
此外,设计良好的类层次结构需要透彻的分析和预见。糟糕的设计可能导致僵化的代码,难以重构。对于小型、简单的任务,面向对象编程可能显得过于繁琐。
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课程辅导,国外大学本科硕士研究生博士课程论文辅导