📚 A-Level CIE Computer Science: Object-Oriented Programming (OOP) Exam Essentials | A-Level CIE 计算机:面向对象考点精讲
Welcome to this focused revision guide on Object-Oriented Programming (OOP) for the Cambridge International A-Level Computer Science syllabus (9618). OOP is a major topic assessed in both Paper 2 (Fundamental Problem-solving and Programming) and Paper 4 (Practical), requiring a deep understanding of classes, objects, inheritance, polymorphism, and design. Mastering these concepts will not only help you tackle exam questions but also prepare you for real-world software development.
欢迎阅读本针对剑桥国际 A-Level 计算机科学大纲(9618)的面向对象编程(OOP)考点精讲。OOP 是卷二(基础问题解决与编程)和卷四(实践)中考查的重要主题,要求你深入理解类、对象、继承、多态和设计。掌握这些概念不仅有助于应对考试题目,也为你的实际软件开发打下基础。
1. Overview of the Object-Oriented Paradigm | 面向对象编程范型概述
OOP models a system as a collection of interacting objects, rather than a sequence of procedural steps. It centres on four main principles: encapsulation, abstraction, inheritance, and polymorphism. Each object combines state (attributes) and behaviour (methods). OOP promotes code reuse, modularity, and easier maintenance, which is why languages like Java, C#, and Python are widely used.
OOP 将系统建模为相互交互的对象的集合,而非过程化的步骤序列。它围绕四大核心原则:封装、抽象、继承和多态。每个对象将状态(属性)和行为(方法)结合在一起。OOP 促进了代码重用、模块化和更易维护,这也是 Java、C# 和 Python 等语言被广泛使用的原因。
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. An object is a concrete instance of a class. For example, a class Student might define attributes name and age, and methods enroll() and getAverageGrade(). To create an object, you use the new keyword: Student s1 = new Student("Alice", 17);. Each object occupies its own memory and holds its own copy of instance variables.
类是一个蓝图或模板,定义了某类对象共有的属性和方法。对象是类的具体实例。例如,类 Student 可能定义属性 name 和 age,以及方法 enroll() 和 getAverageGrade()。要创建对象,使用 new 关键字:Student s1 = new Student("Alice", 17);。每个对象占用自己的内存空间,并拥有自己的实例变量副本。
3. Attributes and Methods | 属性与方法
Attributes (also called fields or instance variables) represent the data stored within an object. They should typically be declared private to uphold encapsulation. Methods define the behaviour of an object and can be categorised as accessors (getters), mutators (setters), and general utility methods. A method signature includes its name, parameter list, and return type. For example, public int getAge() returns an int. Methods can be overloaded if they have the same name but different parameter lists.
属性(也称字段或实例变量)表示对象内部存储的数据。它们通常应声明为 private 以维护封装性。方法定义了对象的行为,可分为访问器(getter)、修改器(setter)和一般工具方法。方法签名包含名称、参数列表和返回类型。例如,public int getAge() 返回一个 int。如果方法名称相同但参数列表不同,则构成方法重载。
4. Constructors | 构造方法
A constructor is a special method invoked when an object is instantiated. It usually initialises instance variables. If no constructor is explicitly written, the compiler provides a default no-argument constructor. You can overload constructors to provide multiple ways of initialising objects. For instance, public Student(String n, int a) { name = n; age = a; }. The keyword this can be used to refer to the current object’s attributes and to call other constructors.
构造方法是对象实例化时调用的特殊方法,通常用于初始化实例变量。如果没有显式编写构造方法,编译器会提供一个默认的无参构造方法。你可以重载构造方法,以提供多种初始化方式。例如,public Student(String n, int a) { name = n; age = a; }。关键字 this 可用于引用当前对象的属性,也可用于调用其他构造方法。
5. Encapsulation and Access Modifiers | 封装与访问修饰符
Encapsulation protects data by restricting direct access from outside the class. Access modifiers control visibility:
封装通过限制外部对数据的直接访问来保护数据。访问修饰符控制可见性:
-
public: accessible from any class.public:可从任何类访问。 -
private: only accessible within the same class.private:仅在同一类内可访问。 -
protected: accessible within the same package and subclasses.protected:在同一包内及子类中可访问。
Proper encapsulation uses private fields and public getters/setters, allowing validation and controlled updates. This makes code more robust and reduces unintended side effects.
正确的封装应该使用私有字段和公共 getter/setter,允许进行验证和受控的更新。这使代码更鲁棒,并减少意外的副作用。
6. Inheritance | 继承
Inheritance allows a new class (subclass or derived class) to absorb the attributes and methods of an existing class (superclass or base class), using the keyword extends. The subclass can add its own attributes and methods, or override inherited methods to provide specialised behaviour. For example, class Teacher extends Person { ... }. The subclass does not inherit constructors, but the superclass constructor can be called using super(). Inheritance supports the “is-a” relationship and reduces code duplication.
继承允许一个新类(子类或派生类)吸收现有类(超类或基类)的属性和方法,使用 extends 关键字。子类可以添加自己的属性和方法,或重写继承的方法以提供特化行为。例如,class Teacher extends Person { ... }。子类不继承构造方法,但可以使用 super() 调用超类的构造方法。继承支持“是一种”的关系,并减少代码重复。
7. Polymorphism | 多态
Polymorphism means “many forms”. There are two main types. Compile-time polymorphism is achieved through method overloading – multiple methods with the same name but different parameters. Runtime polymorphism is achieved through method overriding, where a subclass provides a specific implementation of a method already defined in its superclass. With runtime polymorphism, a superclass reference can point to a subclass object, and the JVM decides which method to call based on the actual object type. For example, Person p = new Teacher(); p.displayInfo(); will call the overridden method in Teacher if it exists. This makes systems more flexible and extensible.
多态意为“
Published by TutorHao | A-Level Computer Science Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导