📚 Object-Oriented Programming Concepts | 面向对象编程概念
Object-oriented programming (OOP) is a paradigm that organises software design around data, or objects, rather than functions and logic. For Edexcel A-Level Computer Science, mastering OOP is essential because it underpins most modern programming languages, including Java, Python and C#. Understanding its core principles – encapsulation, inheritance, polymorphism and abstraction – allows you to write modular, reusable and maintainable code.
面向对象编程(OOP)是一种围绕数据(即对象)而非函数和逻辑来组织软件设计的范式。对于 Edexcel A-Level 计算机科学而言,掌握 OOP 至关重要,因为它是大多数现代编程语言(如 Java、Python 和 C#)的基础。理解其核心原则——封装、继承、多态和抽象——能让你编写出模块化、可重用且易于维护的代码。
1. What is Object-Oriented Programming? | 什么是面向对象编程?
Object-oriented programming is a model that structures a program by bundling related properties and behaviours into individual objects. Unlike procedural programming, which separates data and functions, OOP keeps them together so that an object manages its own state and exposes only selected operations.
面向对象编程是一种通过将相关属性和行为绑定到单个对象中来构建程序的模型。与将数据与函数分离的过程式编程不同,OOP 将它们放在一起,使得对象管理自己的状态并只暴露选定的操作。
In the Edexcel specification, you need to recognise OOP as a high-level paradigm that supports code reusability through classes and objects. An object is a self-contained entity that consists of both data members and member functions, and it communicates with other objects via message passing (method calls).
在 Edexcel 大纲中,你需要认识到 OOP 是一种通过类和对象支持代码可重用性的高级范式。对象是一个自包含的实体,由数据成员和成员函数组成,并通过消息传递(方法调用)与其他对象通信。
2. Classes and Objects | 类与对象
A class is a blueprint or template for creating objects. It defines the attributes (variables) and methods (functions) that an object of that class will have. For example, a class Car might define attributes such as colour and speed, and methods such as accelerate() and brake().
类是创建对象的蓝图或模板。它定义了该类对象将具有的属性(变量)和方法(函数)。例如,一个 Car 类可能定义诸如 colour 和 speed 等属性,以及 accelerate() 和 brake() 等方法。
An object is an instance of a class. When a program runs, the object is created in memory with its own copy of the attributes defined by the class. Many objects can be instantiated from the same class, each with distinct attribute values, yet they share the same behaviour defined by the methods.
对象是类的实例。当程序运行时,对象在内存中被创建,并拥有该类定义的属性的自己的副本。可以从同一个类实例化出多个对象,每个对象具有不同的属性值,但它们共享由方法定义的相同行为。
- Class:
class Student { String name; int age; void study() { ... } } - Object:
Student s1 = new Student(); - Class:
class Student { String name; int age; void study() { ... } } - Object:
Student s1 = new Student();
3. Attributes and Methods | 属性与方法
Attributes, also called fields or instance variables, store the data or state of an object. They are typically defined inside a class and are accessed through the object. In a BankAccount class, for instance, balance and accountNumber would be attributes.
属性,也称为字段或实例变量,存储对象的数据或状态。它们通常在类内部定义,并通过对象访问。例如,在 BankAccount 类中,balance 和 accountNumber 就是属性。
Methods define the behaviour of an object. They are functions that belong to a class and can modify the object’s state or perform computations. A withdraw(amount) method might check if the amount is less than or equal to the balance before deducting it, enforcing business rules inside the object.
方法定义了对象的行为。它们是属于类的函数,可以修改对象的状态或执行计算。一个 withdraw(amount) 方法可能会在扣款前检查金额是否小于或等于余额,从而在对象内部执行业务规则。
4. Encapsulation | 封装
Encapsulation is the mechanism of hiding internal state and requiring all interaction to be performed through an object’s methods. It protects data from unwanted access and modification by marking attributes as private and providing public getter and setter methods.
封装是一种隐藏内部状态并要求所有交互都通过对象方法进行的机制。它通过将属性标记为私有并提供公共的 getter 和 setter 方法来保护数据免受不必要的访问和修改。
In Edexcel examinations, you may be asked to justify encapsulation as a way to achieve data integrity and reduce unintended side effects. For example, a setter for a temperature attribute might reject values below absolute zero, ensuring the object never enters an invalid state.
在 Edexcel 考试中,你可能会被要求说明封装是实现数据完整性和减少意外副作用的一种方式。例如,temperature 属性的 setter 可能会拒绝低于绝对零度的值,从而确保对象永远不会进入无效状态。
- private int age; – hidden from direct access / 禁止直接访问
- public int getAge() { return age; } – controlled read access / 受控读取
- public void setAge(int newAge) { if(newAge>0) age = newAge; } – validation / 验证
5. Inheritance | 继承
Inheritance allows a new class (subclass or derived class) to acquire the attributes and methods of an existing class (superclass or base class). This promotes code reuse and establishes a hierarchical relationship. In many object-oriented languages, a subclass can extend only one superclass (single inheritance).
继承允许一个新类(子类或派生类)获取现有类(超类或基类)的属性和方法。这促进了代码重用并建立了层次关系。在许多面向对象语言中,子类只能扩展一个超类(单继承)。
For instance, a Vehicle superclass may define speed and move(). A Car subclass can inherit these and add its own fuelType attribute and refuel() method. The Car is a Vehicle, which models an “is-a” relationship.
例如,Vehicle 超类可以定义 speed 和 move()。子类 Car 可以继承这些,并添加自己的 fuelType 属性和 refuel() 方法。Car 是 Vehicle,这模拟了“是一种”的关系。
You should be able to draw inheritance diagrams and explain how overriding works in subclasses. Edexcel often expects you to distinguish between inheritance and composition (“has-a” vs “is-a”).
你应该能够绘制继承图并解释子类中的重写是如何工作的。Edexcel 通常期望你区分继承和组合(“有一个”与“是一种”)。
6. Polymorphism | 多态
Polymorphism means “many forms” and enables a subclass to define its own version of a method already provided by its superclass (overriding). It also allows methods to behave differently based on the object that invokes them, even when the object is referenced by a superclass variable.
多态意味着“多种形态”,它使子类能够定义超类已提供的方法的自己的版本(重写)。它还允许方法根据调用它们的对象表现出不同的行为,即使对象是由超类变量引用的。
In practice, a List of Shape objects containing both Circle and Rectangle instances can iterate through and call draw(). Each shape draws itself appropriately without needing conditional logic. This is an example of dynamic binding.
在实践中,一个包含 Circle 和 Rectangle 实例的 Shape 对象列表可以遍历并调用 draw()。每个形状适当地绘制自己,而无需条件逻辑。这是动态绑定的一个例子。
Polymorphism is frequently examined in A-Level papers; be ready to explain method overriding versus method overloading (which is not the same as polymorphic behaviour) and to give code examples showing how parent-class references can point to child-class objects.
多态在 A-Level 试卷中经常出现;准备好解释方法重写与方法重载(与多态行为不同),并给出代码示例,展示父类引用如何指向子类对象。
7. Abstraction | 抽象
Abstraction focuses on exposing only the essential features of an object while hiding the complex implementation details. Abstract classes and interfaces are formal tools to achieve abstraction in many languages such as Java and C#.
抽象侧重于仅暴露对象的基本特征,而隐藏复杂的实现细节。在 Java 和 C# 等许多语言中,抽象类和接口是实现抽象的形式化工具。
An abstract class cannot be instantiated directly; it is designed to be subclassed. It may contain abstract methods (which have no body) that must be implemented in concrete subclasses. This forces a consistent protocol across all derived types.
抽象类不能直接实例化;它被设计为被继承。它可以包含抽象方法(没有方法体),这些方法必须在具体子类中实现。这强制在所有派生类型中建立一致的协议。
For example, an abstract Animal class could declare an abstract method speak(). Each concrete animal such as Dog and Cat would provide its own implementation, e.g., System.out.println(“Woof”) or “Meow”.
例如,一个抽象的 Animal 类可以声明一个抽象方法 speak()。每个具体的动物,比如 Dog 和 Cat,都会提供自己的实现,例如 System.out.println(“Woof”) 或 “Meow”。
8. Constructor Methods | 构造方法
A constructor is a special method invoked when an object is instantiated. It usually initialises the object’s attributes to meaningful initial values. In many languages, the constructor has the same name as the class and does not have a return type.
构造方法是在对象实例化时调用的特殊方法。它通常将对象的属性初始化为有意义的初始值。在许多语言中,构造方法与类同名且没有返回类型。
Multiple constructors can be defined through overloading, allowing objects to be created with different sets of arguments. A default constructor takes no arguments, while a parameterised constructor accepts one or more arguments to set attributes directly.
可以通过重载定义多个构造方法,从而允许使用不同参数集创建对象。默认构造方法不接受参数,而参数化构造方法接受一个或多个参数以直接设置属性。
public Car() { speed = 0; }– default constructor / 默认构造方法public Car(int s) { speed = s; }– parameterised constructor / 参数化构造方法
9. Overriding Methods | 方法重写
Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method signature (name, parameters, return type) remains identical; only the internal behaviour changes.
方法重写发生在子类为其超类中已定义的方法提供特定实现时。方法签名(名称、参数、返回类型)保持不变;只有内部行为发生变化。
Overriding is a key enabler of polymorphic behaviour. When a superclass reference points to a subclass object, the overridden version is executed, not the superclass version. This is resolved at runtime using the object’s actual type.
重写是实现多态行为的关键。当超类引用指向子类对象时,执行的是重写后的版本而非超类版本。这是在运行时根据对象的实际类型来解析的。
Use the @Override annotation in Java to make the intent explicit and avoid accidental errors. In the Edexcel pseudocode notation, overriding is simply shown by redeclaring a superclass method in the subclass.
在 Java 中使用 @Override 注解来明确意图并避免意外错误。在 Edexcel 伪代码表示法中,重写只需在子类中重新声明超类方法即可。
10. Access Modifiers (Public, Private, Protected) | 访问修饰符(公有、私有、受保护)
Access modifiers control the visibility of classes, attributes and methods. In Java and similar languages, public members are accessible from anywhere, private members are only accessible within the same class, and protected members are accessible within the same package and by subclasses.
访问修饰符控制类、属性和方法的可见性。在 Java 等类似语言中,public 成员可以从任何地方访问,private 成员只能在同一个类内访问,而 protected 成员可以在同一个包内以及子类中访问。
Good encapsulation practice makes attributes private and exposes them through public methods. Edexcel questions often ask you to choose the correct modifier for a given design goal, such as preventing external modification while allowing subclass access.
良好的封装实践是将属性设为私有,并通过公共方法暴露它们。Edexcel 问题经常要求你为给定的设计目标选择正确的修饰符,例如在允许子类访问的同时防止外部修改。
| Modifier | Same Class | Subclass | Everywhere |
| public | Yes | Yes | Yes |
| protected | Yes | Yes | No |
| private | Yes | No | No |
11. Static vs Instance Members | 静态成员与实例成员
Members declared as static belong to the class itself rather than to any particular object. A static variable is shared across all instances of the class, and a static method can be called without creating an object. This is helpful for utility functions and constants.
声明为 static 的成员属于类本身,而不是任何特定对象。静态变量在类的所有实例之间共享,而静态方法无需创建对象即可调用。这对工具函数和常量很有帮助。
In Edexcel practical tasks, you might implement a Math utility class or a counter that tracks the number of objects created, both of which rely on static fields. Remember that static methods cannot directly access instance variables or methods.
在 Edexcel 实践任务中,你可能会实现一个 Math 工具类或一个跟踪已创建对象数量的 counter,二者都依赖于静态字段。请记住,静态方法不能直接访问实例变量或方法。
12. Benefits and Drawbacks of OOP | 面向对象编程的优缺点
OOP offers numerous advantages for large-scale software development: modularity (each object is self-contained), reusability (inheritance and composition), extensibility (new subclasses can be added without modifying existing code), and improved security through encapsulation.
OOP 为大规模软件开发提供了诸多优势:模块化(每个对象自包含)、可重用性(继承和组合)、可扩展性(可以添加新的子类而无需修改现有代码),以及通过封装增强的安全性。
However, OOP can introduce overhead: object creation consumes memory, deep inheritance trees can become hard to understand, and over-engineering small programs with classes when a simple procedural script would suffice can reduce efficiency. These trade-offs are often discussed in Edexcel extended-response questions.
然而,OOP 也会带来开销:对象创建消耗内存,深层继承树可能变得难以理解,以及在本可以用简单的过程式脚本时过度用类设计小程序会降低效率。这些权衡经常在 Edexcel 扩展回答问题中讨论。
For A-Level revision, always link theoretical features to practical scenarios: you might use OOP to model a library system, a game character, or a bank transaction, where the object-oriented structure directly mirrors the real-world entities.
对于 A-Level 复习,务必将理论特性与实际场景联系起来:你可以使用 OOP 来模拟图书馆系统、游戏角色或银行交易,其中面向对象结构直接映射现实世界的实体。
Published by TutorHao | Programming Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导