Object-Oriented Programming in IGCSE Computer Science | IGCSE 计算机:面向对象 考点精讲

📚 Object-Oriented Programming in IGCSE Computer Science | IGCSE 计算机:面向对象 考点精讲

Object-oriented programming (OOP) is a paradigm that organises software design around objects rather than functions and logic. An object is a self-contained component that contains data and methods to manipulate that data. For IGCSE Computer Science, understanding OOP helps you write more modular, reusable and maintainable code, aligning closely with how real-world systems are modelled.

面向对象编程是一种以对象而非功能和逻辑为中心来组织软件设计的范式。对象是一个自包含的组件,内含数据以及操作这些数据的方法。对 IGCSE 计算机科学而言,理解面向对象有助于编写更模块化、更具复用性和可维护性的代码,这非常贴近真实系统建模的方式。

1. Understanding Object-Oriented Programming (OOP) | 理解面向对象编程

OOP is built on the idea of modelling software after real-world entities. An object represents a ‘thing’ that has characteristics (attributes) and behaviours (methods). For instance, a ‘Car’ object may have attributes like colour and speed, and methods like accelerate and brake. This mirrors the way humans categorise and interact with the world.

面向对象编程建立在按现实世界实体进行软件建模的思想之上。一个对象代表一个拥有特征(属性)和行为(方法)的“事物”。例如,一个“汽车”对象可能拥有颜色和速度等属性,以及加速和刹车等方法。这映射了人类分类和与世界交互的方式。


2. Classes and Objects | 类与对象

A class is a blueprint or template that defines the structure and behaviours its objects will have. An object is a specific instance of a class. You can think of a class as a cookie cutter, and objects as the cookies it produces. In examination questions, you are often asked to identify the class name and the object identifier from given code.

类是一个蓝图或模板,定义了其对象将拥有的结构和行为。对象是类的一个具体实例。你可以把类想象成一个饼干模具,而对象则是它生产出来的饼干。在考试题目中,经常要求你根据给定的代码识别类名和对象标识符。


3. Attributes and Methods | 属性与方法

Attributes are variables that store an object’s state, such as name, price or ID number. Methods are functions defined within a class that describe the behaviours of an object. Access modifiers like ‘private’ and ‘public’ determine whether an attribute or method can be accessed from outside the class. For IGCSE, you should know how to declare and use both attributes and methods in pseudocode or a high-level language example.

属性是存储对象状态的变量,例如名称、价格或编号。方法是类内部定义的函数,描述对象的行为。诸如 ‘private’ 和 ‘public’ 等访问修饰符决定了属性和方法能否从类外部访问。对 IGCSE 而言,你应该知道如何在伪代码或高级语言示例中声明和使用属性和方法。


4. Encapsulation and Data Hiding | 封装与数据隐藏

Encapsulation bundles data and the methods that operate on that data within one unit, restricting direct access to some of an object’s components. This is often implemented by making attributes private and providing public getter and setter methods. It protects the internal integrity of an object and prevents unintended interference from outside code.

封装将数据以及操作这些数据的方法捆绑在一个单元内,限制对对象某些组成部分的直接访问。这通常通过将属性设为私有并提供公共的获取器和设置器方法来实现。它保护了对象的内部完整性,并防止外部代码的无意干扰。


5. Constructors and Instantiation | 构造方法与实例化

A constructor is a special method that is automatically called when an object is created. It usually assigns initial values to attributes. Instantiation is the process of creating an object from a class using the keyword ‘new’ in many languages. The constructor may accept parameters to set up the object’s state at the moment of creation. For example, ‘new Car(“red”)’ might invoke a constructor that sets the colour attribute to “red”.

构造方法是一种特殊方法,在创建对象时自动调用。它通常为属性分配初始值。实例化是使用许多语言中的 ‘new’ 关键字从类创建对象的过程。构造方法可以接受参数,以便在创建时刻设置对象的状态。例如,’new Car(“red”)’ 可能会调用一个将颜色属性设置为 “red” 的构造方法。


6. Inheritance – ‘is-a’ Relationship | 继承 – “是一个”关系

Inheritance allows a new class to derive properties and methods from an existing class. The existing class is called the superclass (or parent class), and the new class is the subclass (or child class). The subclass inherits all accessible attributes and methods and can also define its own specialised features. For instance, ‘ElectricCar’ inherits from ‘Car’ and adds a battery capacity attribute. This ‘is-a’ relationship (‘ElectricCar is a Car’) promotes code reuse.

继承允许新类从现有类派生属性和方法。现有类称为超类(或父类),新类是子类。子类继承所有可访问的属性和方法,并且还可以定义自己的专有特征。例如,’ElectricCar’ 继承自 ‘Car’ 并添加了一个电池容量属性。这种“是一个”关系(’ElectricCar 是一个 Car’)促进了代码复用。


7. Polymorphism – Many Forms | 多态 – 多种形态

Polymorphism means ‘many forms’ and it allows objects of different classes to be treated as objects of a common superclass. The most common type is method overriding, where a subclass provides its own implementation of a method already defined in the superclass. For example, both ‘Car’ and ‘Bicycle’ might have a ‘move()’ method, but each implements it differently. Polymorphism enables the same interface to be used for a general class of actions.

多态意为“多种形态”,它允许将不同类的对象视为公共超类的对象。最常见的类型是方法重写,即子类提供了自己对超类中已定义方法的实现。例如,’Car’ 和 ‘Bicycle’ 可能都有一个 ‘move()’ 方法,但各自实现方式不同。多态使得能够为某一通用类别的操作使用相同的接口。


8. Advantages of OOP | 面向对象的优势

OOP offers several key advantages: modularity (objects are self-contained), reusability (inheritance and class libraries), ease of maintenance (changes to a class do not affect others if the interface remains stable), and better modelling of complex systems. It also makes large programs easier to develop and debug because the problem can be divided into interacting objects.

面向对象拥有几个关键优势:模块化(对象是独立的)、可复用性(继承和类库)、易于维护(若接口保持稳定,对类的更改不会影响其他部分),以及更好地对复杂系统建模。它还使得大型程序的开发和调试更加容易,因为问题可以被分解为相互交互的对象。


9. OOP vs Procedural Programming | 面向对象与过程化编程对比

Procedural programming focuses on functions and sequences of instructions, often separating data and procedures. OOP, in contrast, combines data and functions into objects. While procedural languages like C use structures and functions, object-oriented languages like Python, Java, and C++ centre on classes and objects. For IGCSE, you might be asked to compare these two paradigms, noting that OOP better supports code reuse and scalability through inheritance and encapsulation.

过程化编程侧重于函数和指令序列,通常将数据与过程分开。相比之下,面向对象将数据和函数组合成对象。诸如 C 等过程化语言使用结构体和函数,而 Python、Java 和 C++ 等面向对象语言以类和对象为中心。对 IGCSE 而言,你可能会被要求比较这两种范式,注意到面向对象通过继承和封装更好地支持了代码复用和可扩展性。


10. Practical Example: A Library System | 实例分析:图书馆系统

Consider a library system. You could have a class ‘Book’ with attributes ‘title’, ‘author’, ‘ISBN’ and methods ‘borrow()’, ‘return()’. Another class ‘Member’ has attributes ‘memberID’, ‘name’, and methods ‘checkOutBook()’. A subclass ‘EBook’ might inherit from ‘Book’ and add a ‘fileSize’ attribute, overriding the ‘borrow()’ method to allow online download. This demonstrates encapsulation, inheritance and polymorphism in a real-world scenario that often appears in exam questions.

考虑一个图书馆系统。你可以有一个 ‘Book’ 类,其属性为 ‘title’、’author’、’ISBN’,方法为 ‘borrow()’、’return()’。另一个类 ‘Member’ 具有属性 ‘memberID’、’name’ 和方法 ‘checkOutBook()’。子类 ‘EBook’ 可以继承 ‘Book’ 并添加 ‘fileSize’ 属性,重写 ‘borrow()’ 方法以允许在线下载。这展示了封装、继承和多态在一个现实世界场景中的应用,该场景经常出现在考题中。


11. Common Pitfalls and Exam Tips | 常见陷阱与考试提示

Students often confuse a class with an object. Remember: a class is the definition; an object is an instance. Also, mixing up ‘private’ and ‘public’ access can lead to encapsulation errors. In exams, you may be asked to write getters and setters, explain why private attributes are used, or identify missing inheritance arrows in a class diagram. Practise reading and writing simple class definitions, and always use the correct terminology.

学生们经常混淆类和对象。请记住:类是定义;对象是实例。此外,混淆 ‘private’ 和 ‘public’ 访问权限可能导致封装错误。在考试中,你可能会被要求编写获取器和设置器,解释为何使用私有属性,或者识别类图中缺失的继承箭头。练习阅读和编写简单的类定义,并且始终使用正确的术语。

Published by TutorHao | Computer Science Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading