📚 Object-Oriented Programming for Edexcel A-Level Computer Science | Edexcel A-Level 编程:面向对象编程核心考点
Object-oriented programming (OOP) is a central topic in Edexcel A-Level Computer Science. You need to be able to explain classes, objects, inheritance, polymorphism, encapsulation and composition, and to apply these ideas to short scenarios and pseudocode questions.
面向对象编程(OOP)是 Edexcel A-Level 计算机科学的核心主题。你需要能够解释类、对象、继承、多态、封装和组合,并能将这些思想应用到短场景和伪代码题中。
1. Programming Paradigms and Why OOP Matters | 编程范式与面向对象的意义
A programming paradigm is a fundamental style of programming. Procedural programming structures a program as sequences of instructions and functions, while object-oriented programming groups state and behaviour into objects.
编程范式是一种基本的编程风格。面向过程编程将程序组织为指令序列和函数,而面向对象编程则将状态和行为封装在对象中。
Edexcel expects you to compare paradigms. OOP is especially useful for large, maintainable systems because objects model real-world entities and reduce coupling between data and functions.
Edexcel 要求你比较不同范式。OOP 对大型可维护系统特别有用,因为对象模拟现实世界实体,并减少数据与函数之间的耦合。
In exam short-answer questions, link a paradigm to a scenario: for a graphical user interface or a game with many interacting entities, OOP is often a strong justification.
在考试简答题中,要把范式与场景联系起来:对于图形用户界面或包含许多交互实体的游戏,OOP 通常是很有力的选择理由。
You should also recognise that event-driven, functional and procedural approaches are not mutually exclusive: many modern programs combine paradigms where appropriate.
你还要认识到事件驱动、函数式和面向过程的方法并不是互斥的:许多现代程序会在合适的地方组合使用不同范式。
2. Classes and Objects | 类与对象
In OOP, a class is a template that defines the attributes and methods shared by a group of objects. An object is a concrete instance created from the class.
在面向对象中,类是一个模板,定义一组对象共有的属性和方法。对象是由类创建的具体实例。
For example, a class Dog might declare attributes name and age, and a method bark(). Writing Dog my_dog = new Dog() creates one object with its own state.
例如,类 Dog 可以声明属性 name 和 age,以及方法 bark()。写下 Dog my_dog = new Dog() 会创建一个拥有自己状态的对象。
In Edexcel pseudocode and Python examples, be able to identify which identifier is the class and which is the object. The class is written once; many objects can be instantiated.
在 Edexcel 伪代码和 Python 示例中,要能分辨哪个标识符是类、哪个是对象。类只编写一次,但可以实例化许多对象。
Objects have three key features: identity, state and behaviour. Identity distinguishes one object from another, state is held in attributes, and behaviour is provided by methods.
对象有三个关键特征:身份、状态和行为。身份区分不同对象,状态保存在属性中,行为由方法提供。
3. Attributes and Methods | 属性与方法
Attributes store an object’s data, such as name, score or position. Methods define the operations an object can perform, often reading or changing these attributes.
属性存储对象的数据,例如姓名、分数或位置。方法定义对象可以执行的操作,通常会读取或修改这些属性。
In a class diagram, attributes are usually shown in the second compartment and methods in the third. Edexcel questions may ask you to interpret or draw simple UML-style class diagrams.
在类图中,属性通常显示在第二个区域,方法显示在第三个区域。Edexcel 考题可能要求你解释或绘制简单的 UML 风格类图。
Accessors such as get_name() and mutators such as set_name(value) are used to read and update private attributes safely.
访问器如 get_name() 和修改器如 set_name(value) 用于安全地读取和更新私有属性。
In pseudocode, show method signatures clearly. A constructor is written separately from ordinary methods because it has no return type in most languages and is called automatically at instantiation.
在伪代码中,要清楚展示方法签名。构造器与普通方法分开书写,因为它在大多数语言中没有返回类型,并且在实例化时自动调用。
4. Encapsulation and Information Hiding | 封装与信息隐藏
Encapsulation means bundling data and the methods that operate on that data into a single unit, while hiding the internal implementation from outside code.
封装意味着将数据和操作这些数据的方法捆绑在一个单元中,同时对外部代码隐藏内部实现。
In practice, attributes are declared private and outside code interacts through public methods. This allows validation, prevents invalid states, and makes future changes less disruptive.
实践中,属性被声明为私有,外部代码通过公有方法交互。这样可以进行验证、防止无效状态,并使未来修改影响更小。
Avoid saying a private method ‘cannot be accessed’ without qualification. It can be accessed inside the class, but not directly from outside the object.
不要不加限定地说私有方法“无法访问”。它可以在类内部访问,但不能从对象外部直接访问。
Edexcel may ask how encapsulation improves maintainability. Explain that internal changes do not affect other code as long as the public interface stays the same.
Edexcel 可能问封装如何提高可维护性。可以解释:只要公有接口不变,内部修改就不会影响其他代码。
5. Inheritance and Class Hierarchies | 继承与类层次
Inheritance allows a derived class to acquire the attributes and methods of a base class. The relationship should always be an ‘is-a’ relationship, such as Dog is an Animal.
继承允许派生类获取基类的属性和方法。这种关系必须始终是“is-a”关系,例如 Dog is an Animal。
A subclass can add new members and can override inherited methods to provide specialised behaviour. In Edexcel exams, identify the base class, derived class, and overridden method from a short scenario.
子类可以添加新成员,也可以重写继承的方法以提供专门行为。在 Edexcel 考试中,要从简短场景中识别基类、派生类和重写的方法。
Be careful not to confuse inheritance with composition: inheritance models ‘is-a’, while composition models ‘has-a’.
注意不要把继承和组合混淆:继承表示“is-a”,组合表示“has-a”。
In a class hierarchy, common features are placed high in the hierarchy, reducing duplication. However, deep hierarchies can become difficult to understand and test.
在类层次中,共同特征被放在较高层,从而减少重复。但是过深的层次结构可能变得难以理解和测试。
6. Polymorphism and Method Overriding | 多态与方法重写
Polymorphism means that the same method call can behave differently depending on the actual object type. This is usually achieved through method overriding in a class hierarchy.
多态意味着同一个方法调用可以根据实际对象类型表现不同行为。这通常通过类层次中的方法重写实现。
If a list holds Animal references but contains Dog and Cat objects, calling a.speak() resolves at runtime to the appropriate subclass method. This is dynamic binding.
如果列表保存 Animal 引用但包含 Dog 和 Cat 对象,调用 a.speak() 会在运行时解析到相应的子类方法。这就是动态绑定。
Edexcel uses compile-time vs runtime decision questions. Emphasise that polymorphism depends on runtime object type, not the declared reference type.
Edexcel 可能考查编译时与运行时决策。要强调多态取决于运行时对象类型,而不是声明的引用类型。
Method overriding keeps the same signature but changes the implementation. Method overloading uses the same name but different parameters; Edexcel mainly tests overriding, so check the wording carefully.
方法重写保持相同的签名但改变实现。方法重载使用相同名称但不同参数;Edexcel 主要考查重写,因此要仔细审题。
7. Composition and Aggregation | 组合与聚合
Composition models a ‘has-a’ relationship where the whole owns its parts. For example, a Car has an Engine, and the engine normally cannot exist independently in the program.
组合表示“has-a”关系,即整体拥有部分。例如,Car has an Engine,程序中引擎通常不能独立存在。
Aggregation is a weaker form of ‘has-a’ in which parts can exist independently of the whole, such as a Library containing Member objects.
聚合是一种较弱的“has-a”关系,部分可以独立于整体存在,例如 Library 包含 Member 对象。
In class diagrams, composition is shown by a filled diamond, aggregation by a hollow diamond. Edexcel may ask you to justify which relationship is more appropriate from a scenario.
在类图中,组合用实心菱形表示,聚合用空心菱形表示。Edexcel 可能要求你根据场景说明哪种关系更合适。
Use composition when the lifetime of the part depends on the whole. Use aggregation when the part can be shared or can outlive the whole.
当部分的生命周期依赖于整体时使用组合。当部分可以被共享或比整体存续更久时使用聚合。
8. Constructors and Object Lifecycle | 构造器与对象生命周期
A constructor is a special method used to initialise an object when it is instantiated. It usually sets initial values for attributes.
构造器是一种特殊方法,用于在实例化对象时进行初始化。它通常为属性设置初始值。
Parameterised constructors allow different objects to be created with different starting states. In pseudocode, show how values are passed into the constructor.
带参数的构造器允许创建具有不同初始状态的对象。在伪代码中,要展示如何将值传入构造器。
In Python, the __init__ method is the constructor. Questions may ask you to write or complete a constructor that assigns parameters to attributes.
在 Python 中,__init__ 方法是构造器。考题可能要求你编写或补全一个将参数赋给属性的构造器。
If no constructor is written, many languages provide a default constructor that sets attributes to zero, empty strings or null. This can cause subtle errors if you expect meaningful initial values.
如果没有编写构造器,许多语言会提供默认构造器,将属性设为 0、空字符串或 null。如果你期望有意义的初始值,这可能导致难以察觉的错误。
9. Static and Class Members | 静态成员与类成员
Static members belong to the class itself rather than to any individual object. A static attribute such as number_of_dogs is shared by all instances.
静态成员属于类本身,而不是任何单个对象。静态属性如 number_of_dogs 由所有实例共享。
Static methods can be called without creating an object and cannot directly access instance attributes unless an object is passed in.
静态方法无需创建对象即可调用,不能直接访问实例属性,除非传入对象。
Edexcel may ask you to distinguish class attributes from instance attributes in a trace table or short code snippet.
Edexcel 可能要求你在跟踪表或简短代码片段中区分类属性与实例属性。
Use static members for values that logically belong to the class as a whole, such as a counter of objects created or a shared constant like PI.
静态成员用于逻辑上属于整个类的值,例如已创建对象的计数器或像 PI 这样的共享常量。
10. OOP Design Principles and Exam Technique | 面向对象设计原则与考试技巧
Advantages of OOP include reusability through inheritance, easier maintenance through encapsulation, and natural modelling of real-world entities. Disadvantages include overhead, steep learning curve, and potential over-engineering for small programs.
OOP 的优点包括通过继承实现复用、通过封装更易维护,以及对现实世界实体的自然建模。缺点包括开销、学习曲线陡峭,以及对小型程序可能过度设计。
In longer Edexcel questions, structure your answer by identifying the classes, their major attributes and methods, the relationships between them, and how encapsulation protects data.
在较长的 Edexcel 题目中,组织答案时要识别类、它们的主要属性和方法、类之间的关系,以及封装如何保护数据。
Always quote scenario evidence: ‘Because a Customer has an Account, composition/aggregation is appropriate’, rather than giving generic definitions only.
回答时始终引用场景证据:“因为 Customer 拥有 Account,所以聚合/组合是合适的”,而不要只给通用定义。
Practice converting short scenarios into class diagrams and then into pseudocode. Marks are awarded for correct class naming, sensible attributes, correct relationships, and appropriate access modifiers.
练习将简短场景转换为类图,再转换为伪代码。评分点通常包括正确的类命名、合理的属性、正确的关系以及合适的访问修饰符。
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课程辅导,国外大学本科硕士研究生博士课程论文辅导