📚 Object-Oriented Programming for A-Level WJEC Computer Science | A-Level WJEC 计算机:面向对象 考点精讲
Object-oriented programming (OOP) is a cornerstone of the WJEC A-Level Computer Science specification. It provides a powerful paradigm for organising code around objects that contain both data and behaviour. Mastering OOP concepts such as classes, encapsulation, inheritance and polymorphism is essential for achieving high marks in both the written examination and the practical programming project. This revision guide breaks down every key topic into exam-focused explanations, complete with examples and terminology required by the WJEC board.
面向对象编程(OOP)是WJEC A-Level计算机科学考试的核心内容。它提供了一种强大的编程范式,围绕着同时包含数据与行为的对象来组织代码。掌握类、封装、继承和多态等OOP概念,对于在笔试和实践编程项目中取得高分至关重要。本备考指南将每个关键主题拆解为紧扣考点的讲解,并配有WJEC考试局所要求的示例和术语。
1. Understanding Classes and Objects | 理解类和对象
A class is a blueprint or template that defines the attributes and methods common to a group of objects. It describes what an object will look like and how it will behave, but does not allocate memory on its own. For example, a Car class might define properties such as colour, make and current speed, along with methods like accelerate and brake.
类是定义一组对象共有的属性和方法的蓝图或模板。它描述了对象的外观和行为,但自身并不分配内存。例如,一个Car类可以定义颜色、品牌和当前速度等属性,以及加速和刹车等方法。
An object is a specific instance of a class, created at runtime. Each object has its own state, stored in instance variables. When you write myCar = Car("red", "Toyota"), you instantiate the class and myCar becomes a concrete instance with its own copy of the attributes. The distinction between class and object is frequently tested in WJEC questions that ask you to explain terms or to read short code snippets.
对象是类在运行时所创建的具体实例。每个对象都有自己的状态,保存在实例变量中。当你写myCar = Car("red", "Toyota")时,便实例化了该类,myCar成为一个具有自己属性副本的具体实例。类与对象的区别经常出现在WJEC考题中,要求解释术语或阅读简短代码片段。
Key terminology: instantiation, instance variable, instance. Remember: a class is a logical construct; an object occupies memory.
关键术语:实例化、实例变量、实例。记住:类是逻辑结构;对象占用内存。
2. Attributes and Methods | 属性与方法
Attributes (also called fields or data members) represent the state of an object. They are typically declared as variables inside a class. In the WJEC specification, it is important to know the difference between instance attributes and class-level attributes (static fields). Instance attributes hold data unique to each object, whereas static attributes are shared across all objects of the class.
属性(也称字段或数据成员)代表对象的状态。它们通常被声明为类内部的变量。在WJEC大纲中,了解实例属性与类级属性(静态字段)的区别很重要。实例属性保存每个对象特有的数据,而静态属性在类的所有对象之间共享。
Methods define the behaviour of an object. They are functions written inside a class that can access and modify the object’s attributes. An accessor (or getter) method returns an attribute value without altering the object’s state, while a mutator (or setter) method updates an attribute. In the exam, you may be asked to write or identify accessor/mutator signatures and to explain why certain methods are needed for encapsulation.
方法定义了对象的行为。它们是写在类内部的函数,可以访问和修改对象的属性。访问器(getter)方法返回属性值而不改变对象状态,而修改器(setter)方法则更新属性。在考试中,你可能需要编写或识别访问器/修改器的签名,并解释为什么某些方法对于封装是必需的。
For a class BankAccount with attribute balance, typical methods would include getBalance() and deposit(amount). Note that WJEC pseudocode uses specific notations for method definitions and calls, so be comfortable with both pseudocode and a chosen high-level language.
对于一个包含属性balance的BankAccount类,典型的方法包括getBalance()和deposit(amount)。请注意,WJEC的伪代码在方法定义和调用中使用特定符号,因此要熟练掌握伪代码和你所选择的高级语言。
3. Constructors | 构造方法
A constructor is a special method that is automatically called when an object is created. Its main role is to initialise the attributes of an object to valid starting values. In many languages, the constructor has the same name as the class and does not specify a return type. Overloading constructors — providing multiple versions with different parameter lists — is a common technique to offer flexibility when instantiating objects.
构造方法是一种特殊方法,在创建对象时自动调用。其主要作用是将对象的属性初始化为有效的初始值。在许多语言中,构造函数与类同名,且不指定返回类型。重载构造方法——提供多个参数列表不同的版本——是一种常用技术,以便在实例化对象时提供灵活性。
WJEC questions often present an incomplete class diagram or code segment and ask you to write a constructor that initialises given attributes. Marks are awarded for correct header, correct parameter assignment (e.g. this.name = name) and handling of default values. Be careful to distinguish between a default constructor (no parameters, provided by the compiler unless explicitly defined) and a parameterised constructor.
WJEC试题通常给出一个不完整的类图或代码段,要求写出初始化给定属性的构造方法。正确的方法头部、正确的参数赋值(例如this.name = name)以及处理默认值都能得分。要注意区分默认构造方法(无参数,除非显式定义,否则由编译器提供)和带参构造方法。
When writing constructors in exam code, always initialise every attribute to avoid undefined states even if the question seems to focus on only a subset of attributes.
在考试代码中编写构造方法时,始终初始化每个属性以避免未定义状态,即使题目似乎只关注部分属性。
4. Encapsulation and Access Modifiers | 封装与访问修饰符
Encapsulation is the principle of bundling data and the methods that operate on that data within a single unit, and restricting direct access to some of an object’s components. In practice, this is achieved by making attributes private and providing public getters and setters to control access. Encapsulation helps protect an object’s internal state from unintended interference and makes code more maintainable.
封装是将数据及操作数据的方法捆绑在一个单元内,并限制对对象某些组成部分的直接访问的原则。在实践中,通过将属性设为私有(private)并提供公共(public)的getter和setter来控制访问,从而实现封装。封装有助于保护对象的内部状态免受意外干扰,并使代码更易于维护。
WJEC expects you to know the common access modifiers: public, private and protected. public members are accessible from any code; private members are only accessible within the same class; protected members are accessible within the same class and by subclasses. You may be asked to justify why an attribute should be private, linking your answer to validation or security requirements.
WJEC要求了解常见的访问修饰符:public、private和protected。public成员可从任何代码访问;private成员只能在同一个类中访问;protected成员可在同一个类及其子类中访问。你可能需要论证为什么某个属性应该是私有的,并将答案与验证或安全性要求联系起来。
In exam scenarios, you might see a class definition with attributes declared as private and be asked to explain the advantage. Simply stating “it prevents outside code from changing the value directly” would gain marks; extending the answer to explain that the setter can include validation logic (e.g. age cannot be negative) would earn higher marks.
在考试情境中,你可能会看到一个属性声明为private的类定义,并被要求解释这样做的优点。仅仅陈述“它防止外部代码直接更改值”可得基础分;进一步解释setter可以包含验证逻辑(例如年龄不能为负)会获得更高分数。
5. Inheritance | 继承
Inheritance allows a new class to be based on an existing class, inheriting its attributes and methods. The existing class is called the superclass (or parent), and the new class is the subclass (or child). This promotes code reuse and establishes a hierarchical relationship. The keyword extends is used in many languages to indicate inheritance; in class diagrams, a hollow triangle arrow points from subclass to superclass.
继承允许新类基于现有类,继承其属性和方法。现有类称为超类(或父类),新类称为子类。这促进了代码重用并建立了层次关系。许多语言中使用关键词extends表示继承;在类图中,空心三角箭头从子类指向超类。
WJEC questions frequently involve class diagrams that show inheritance relationships. You need to be able to identify which methods are inherited, which are overridden, and which new methods are added. For example, a Mammal superclass may have a method giveBirth(); the subclass Dog inherits it without change, but the subclass Platypus might override it because platypuses lay eggs. Always think about the “is-a” relationship: a dog is a mammal. If the relationship does not satisfy “is-a”, inheritance is probably misused.
WJEC试题经常涉及展示继承关系的类图。你需要能够识别哪些方法被继承、哪些被重写、以及添加了哪些新方法。例如,超类Mammal可能有一个方法giveBirth();子类Dog不变地继承它,但子类Platypus可能会重写它,因为鸭嘴兽产卵。始终考虑“是一个”关系:狗是一个哺乳动物。如果关系不满足“是一个”,继承可能被误用。
Important: always check whether a subclass method uses the same signature as a superclass method — that indicates method overriding, not simple inheritance. Marks are given for correct use of terminology such as superclass, subclass, override, and extends.
重要提示:始终检查子类方法是否使用与超类方法相同的签名——这表明是方法重写,而非简单继承。正确使用超类、子类、重写和extends等术语能得分。
6. Polymorphism and Method Overriding | 多态与方法重写
Polymorphism means “many forms” and allows objects of different classes to respond to the same method call in different ways. In WJEC A-Level, the focus is on runtime polymorphism achieved through overriding. When a superclass reference points to a subclass object and an overridden method is called, the actual method executed is determined by the object’s type at runtime, not the reference type. This is called dynamic method dispatch.
多态意为“多种形态”,它允许不同类的对象以不同方式响应相同的方法调用。在WJEC A-Level中,重点是重写实现的运行时多态。当超类引用指向子类对象并且调用被重写的方法时,实际执行的方法由运行时对象的类型决定,而不是引用类型。这称为动态方法调度。
Method overriding occurs when a subclass provides its own version of a method that is already defined in its superclass. The overriding method must have the same name, return type and parameter list. Use the @Override annotation (if using Java) to make the intention clear. Questions might present a scenario where an array of superclass type holds various subclasses, and a loop calls a common method such as calculateArea(). You should predict which subclass implementations are executed and explain why it demonstrates polymorphism.
方法重写发生在子类提供自己在超类中已定义方法的版本时。重写方法必须具有相同的名称、返回类型和参数列表。使用@Override注解(若使用Java)可以使意图清晰。考题可能呈现一个场景,其中超类类型的数组包含各种子类,并通过循环调用公共方法如calculateArea()。你需要预测执行哪些子类实现,并解释为什么这展示了多态。
Be precise: overloading is not the same as polymorphism in the WJEC sense. Overloading occurs when two methods share the same name but have different parameter lists (compile-time decision). Polymorphism, in contrast, relies on overriding and works at runtime.
请精确:重载与WJEC定义的多态不同。重载发生在两个方法同名但参数列表不同时(编译时决定)。**多态**则依赖重写并在运行时工作。
7. Abstract Classes | 抽象类
An abstract class is a class that cannot be instantiated on its own and is designed to serve as a superclass for other classes. It may contain both fully-implemented methods and abstract methods — methods that have a signature but no body. Subclasses must provide implementations for all inherited abstract methods, or they themselves become abstract. The purpose is to define a common interface while forcing subclasses to supply specific behaviour.
抽象类是无法自行实例化的类,旨在作为其他类的超类。它可以包含完全实现的方法和抽象方法——即有签名但无方法体的方法。子类必须为所有继承的抽象方法提供实现,否则自身也会成为抽象类。其目的是定义一个公共接口,同时强制子类提供特定行为。
In WJEC exam papers, you may be shown a class diagram where some class names are in italics (UML convention for abstract classes) or methods are marked with «abstract». You should recognise that these classes cannot be used with the new keyword. Typical use case: an abstract class Shape might declare an abstract method double area() and non-abstract method showType(). Concrete subclasses Circle and Rectangle must each provide their own area() implementation.
在WJEC试卷中,你可能会看到类图中某些类名以斜体显示(UML中表示抽象类的约定),或者方法标有«abstract»。你应该识别出这些类不能使用new关键字。典型用例:抽象类Shape可能声明一个抽象方法double area()和一个非抽象方法showType()。具体子类Circle和Rectangle必须各自提供自己的area()实现。
Explain in your answers that abstract classes support partial implementation and can have instance variables, unlike interfaces (in languages where interfaces lack attributes). This distinction is a common comparison question.
在你的答案中要解释,与接口不同(在那些接口没有属性的语言中),抽象类支持部分实现且可以拥有实例变量。这一区别是常见的比较题。
8. Interfaces | 接口
An interface defines a contract that implementing classes must fulfil. In many languages, an interface contains only method signatures (no implementation) and constant declarations. A class that implements an interface must provide bodies for all of the interface’s methods. This promotes a “can-do” relationship rather than the “is-a” relationship of inheritance. For instance, a Bird class and an Airplane class might both implement a Flyable interface.
接口定义了实现类必须履行的契约。在许多语言中,接口仅包含方法签名(没有实现)和常量声明。实现接口的类必须为接口中的所有方法提供方法体。这促进了“能做”的关系,而非继承的“是一个”关系。例如,Bird类和Airplane类可能都实现了Flyable接口。
WJEC expects you to understand that interfaces enable a form of multiple inheritance of behaviour, because a class can implement multiple interfaces, even if the language does not support multiple class inheritance. In pseudocode, interfaces may be shown with the keyword interface, and implementation with implements. Typical exam tasks involve completing a class that implements a given interface, ensuring all methods are present with matching signatures.
WJEC希望你理解接口实现了一种行为上的多重继承形式,因为一个类可以实现多个接口,即使语言不支持多类继承。在伪代码中,接口可能用关键词interface表示,实现用implements表示。典型的考试任务包括补全实现给定接口的类,确保所有方法都以匹配的签名存在。
When comparing abstract classes and interfaces, state that abstract classes can provide common code, while interfaces define pure capability. Modern languages sometimes blur this line (e.g. default methods in Java), but for WJEC, the traditional distinction holds.
在比较抽象类和接口时,要说明抽象类可以提供公共代码,而接口定义纯粹的能力。现代语言有时模糊了这一界限(例如Java中的默认方法),但对于WJEC来说,传统区别仍然成立。
9. Association, Aggregation and Composition | 关联、聚合和组合
Beyond inheritance, classes can be related through associations that describe how objects are connected. Association is a general relationship where one class uses or interacts with another. Aggregation is a specialised form of association indicating a “has-a” relationship where the part can exist independently of the whole; for example, a Department can exist without a University. Composition is a stronger form of aggregation in which the part cannot exist independently; for example, a Room is part of a House and is destroyed when the house is destroyed.
除了继承,类还可以通过描述对象间如何连接的关联来建立关系。关联是一种一般关系,其中一个类使用或与另一个类交互。聚合是一种专门的关联形式,表示“有一个”关系,其中部分可以独立于整体存在;例如,Department可以脱离University存在。组合是一种更强的聚合形式,其中部分不能独立存在;例如,Room是House的一部分,当House被销毁时Room也会被销毁。
WJEC class diagram questions may feature annotated lines: a simple line for association, an empty diamond for aggregation, and a filled diamond for composition. Be prepared to explain the multiplicity notation (e.g. 1..* or 0..1) and translate between diagram and code. In code, composition is typically implemented by creating an object of the component class inside the composite class and not exposing it to the outside.
WJEC类图题可能带有注释连线:简单连线表示关联,空心菱形表示聚合,实心菱形表示组合。准备好解释多重性符号(例如1..*或0..1),并在图表和代码之间转换。在代码中,组合通常通过在复合类内部创建组件类的对象,并且不向外部暴露它来实现。
Avoid confusing aggregation/composition with inheritance. Inheritance is an “is-a” relationship; aggregation and composition are “has-a” relationships. This distinction is a favourite for multiple-choice questions.
避免将聚合/组合与继承混淆。继承是“是一个”关系;聚合和组合是“有一个”关系。这一区别是选择题的常见考点。
10. Static Members and Class Methods | 静态成员和类方法
Static members belong to the class itself rather than to any particular instance. A static variable is shared by all objects of the class; a static method can be called without creating an object, and it can only directly access other static members. Use cases include utility functions, counting the number of instances created, and defining constants.
静态成员属于类本身而非任何特定实例。静态变量被类的所有对象共享;静态方法无需创建对象即可调用,并且它只能直接访问其他静态成员。用例包括实用函数、统计已创建的实例数量以及定义常量。
In the exam, you might be asked to identify why a method is marked as static, or to write a static method that, for example, computes the average of an array. Remember: static methods cannot access instance variables directly, because there is no associated instance. In your pseudocode, use the keyword static or the notation suitable for WJEC, and refer to static members by class name (e.g. Math.PI).
在考试中,你可能会被要求识别某个方法为何标记为静态,或编写一个静态方法,例如计算数组的平均值。记住:静态方法不能直接访问实例变量,因为没有关联的实例。在你的伪代码中使用关键词static或适合WJEC的符号,并通过类名引用静态成员(例如Math.PI)。
Static initialisation blocks and the concept of a static context may also appear in high-band questions. You do not need to go into extraordinary depth, but a clear understanding of lifetime (class loading vs. instance creation) will help.
静态初始化块和静态上下文的概念也可能出现在高分段题目中。你不需要非常深入,但清楚理解生命周期(类加载 vs. 实例创建)会有所帮助。
11. Exam-Style Questions and Marking Tips | 考试题型及评分技巧
WJEC A-Level Computer Science papers typically include a mix of short-answer, structured and extended-response questions on OOP. You might be asked to define terms such as class, object, encapsulation or polymorphism, each worth 1-2 marks. For full marks, you must use precise technical language and, where appropriate, provide a short example. For instance, “Encapsulation is the practice of keeping fields private and providing public getters and setters to control access to an object’s data.”
WJEC A-Level计算机科学试卷通常包含关于OOP的简答题、结构化题和扩展题。你可能需要定义术语,如类、对象、封装或多态,每题1-2分。要得满分,必须使用精确的技术语言,并在适当情况下提供简短示例。例如,“封装是将字段保持为私有并提供公共getter和setter以控制对对象数据的访问的实践。”
Extended writing questions often require you to compare two concepts (e.g. abstract class vs. interface, or overriding vs. overloading) or to explain how a given scenario can be modelled with OOP principles. Structure your answer with clear headings or bullet points (if allowed), and always relate back to the scenario. Marks are allocated for identifying the correct relationship, drawing a rough class diagram in text, or suggesting method signatures.
扩展写作题经常要求你比较两个概念(例如抽象类 vs. 接口,或重写 vs. 重载),或解释如何用OOP原则对给定场景建模。用清晰的标题或项目符号组织你的答案(如果允许),并始终与场景相关联。分数分配用于识别正确的关系、用文字画出粗略的类图或建议方法签名。
When writing code in the exam, follow the WJEC pseudocode conventions exactly. If the question allows a high-level language, add comments to explain your logic. Use meaningful identifier names. Even if your code has a minor syntax error, the logic can still earn marks if it is clear. Always initialise variables before use and handle potential edge cases where possible.
在考试中编写代码时,务必严格遵循WJEC伪代码约定。如果题目允许使用高级语言,请添加注释以解释逻辑。使用有意义的标识符名称。即使你的代码有微小的语法错误,只要逻辑清晰,仍可获得分数。始终在使用前初始化变量,并尽可能处理潜在的边界情况。
Finally, practice with past papers and mark schemes. Notice how marks are awarded for stating assumptions, explaining the ‘why’, and linking OOP features to the quality attributes of software — maintainability, reusability, and security.
最后,通过历年试卷和评分方案进行练习。注意如何因陈述假设、解释“为什么”以及将OOP特性与软件的质量属性(可维护性、可重用性和安全性)联系起来而得分。
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课程辅导,国外大学本科硕士研究生博士课程论文辅导