Object-Oriented Programming | 面向对象编程

📚 Object-Oriented Programming | 面向对象编程

Object-Oriented Programming (OOP) is a paradigm that organises software design around data, or objects, rather than functions and logic. It enables programmers to model real-world entities, making code easier to maintain, reuse and extend. In A-Level Edexcel Computer Science, understanding OOP is essential, as it underpins many modern programming languages and software engineering principles. This article explains the core concepts of OOP with clear examples and highlights their relevance to the Edexcel specification.

面向对象编程(OOP)是一种以数据(即对象)而非功能与逻辑为核心的软件设计范式。它让程序员能够对现实世界中的实体进行建模,从而使代码更易于维护、复用和扩展。在 Edexcel A-Level 计算机科学课程中,理解面向对象编程至关重要,因为它是许多现代编程语言和软件工程原则的基础。本文将通过清晰的示例解释 OOP 的核心概念,并突出其在 Edexcel 大纲中的相关性。

1. Introduction to OOP | 面向对象编程导论

OOP is a programming paradigm centred on the concept of objects, which are instances of classes. A class acts as a blueprint, defining the properties (attributes) and behaviours (methods) that its objects will have. Unlike procedural programming, which separates data from procedures, OOP bundles them together, leading to better organisation and modularity. This approach mirrors how humans perceive the world: as collections of interacting entities.

面向对象编程是一种以对象(类的实例)为核心的编程范式。类相当于蓝图,定义了其对象将具有的属性(数据)和行为(方法)。与将数据与过程分离的过程式编程不同,OOP 将它们捆绑在一起,从而提高了组织性和模块化程度。这种方法反映了人类感知世界的方式:世界是由相互作用的实体组成的。


2. Classes and Objects | 类与对象

A class is a template for creating objects. For example, a class Car might have attributes like colour and speed, and methods like accelerate() and brake(). An object is a specific instance of the class, such as myCar = Car("red", 0). Each object has its own copy of the attributes, so myCar.colour could be “red” while anotherCar.colour is “blue”.

类是创建对象的模板。例如,Car 类可能具有 colorspeed 等属性,以及 accelerate()brake() 等方法。对象是该类的具体实例,例如 myCar = Car("red", 0)。每个对象都有自己的属性副本,因此 myCar.color 可能是 “red”,而 anotherCar.color 是 “blue”。

class Car:
    def __init__(self, colour, speed):
        self.colour = colour
        self.speed = speed

    def accelerate(self, increment):
        self.speed += increment

3. Attributes and Methods | 属性与方法

Attributes store the state of an object. They can be public, private or protected depending on the language’s access modifiers. Methods define the object’s behaviour and often operate on the attributes. For instance, a BankAccount class might have a private attribute balance and public methods deposit() and withdraw() to ensure controlled access to the balance.

属性存储对象的状态。根据语言的访问修饰符,它们可以是公有的、私有的或受保护的。方法定义了对象的行为,通常操作这些属性。例如,BankAccount 类可能有一个私有属性 balance,以及公有方法 deposit()withdraw(),以确保对余额的访问受到控制。


4. Encapsulation | 封装

Encapsulation is the bundling of data with the methods that operate on that data, and restricting direct access to some of an object’s components. This is typically achieved by making attributes private and providing public getter and setter methods. Encapsulation protects the integrity of the data and hides the internal implementation details from the outside world. In Edexcel A-Level, you’ll encounter encapsulation as a key principle that reduces complexity and prevents accidental interference.

封装是将数据与操作该数据的方法捆绑在一起,并限制对对象某些组件的直接访问。这通常通过将属性设为私有并提供公有的 getter 和 setter 方法来实现。封装保护了数据的完整性,并向外部隐藏了内部实现细节。在 Edexcel A-Level 课程中,你将学到封装是降低复杂性并防止意外干扰的关键原则。

class Student:
    def __init__(self, name):
        self.__name = name   # private attribute

    def get_name(self):
        return self.__name

    def set_name(self, new_name):
        if new_name:
            self.__name = new_name

5. Inheritance | 继承

Inheritance allows a class (subclass) to inherit attributes and methods from another class (superclass). This promotes code reuse and establishes a natural hierarchy. For example, a Dog class can inherit from an Animal class, gaining basic methods like eat() while adding its own bark() method. In the Edexcel specification, you need to be able to identify superclasses and subclasses and understand how inheritance supports polymorphism.

继承允许一个类(子类)继承另一个类(父类)的属性和方法。这促进了代码复用,并建立了自然的层次结构。例如,Dog 类可以从 Animal 类继承,获得 eat() 等基本方法,同时添加自己的 bark() 方法。在 Edexcel 大纲中,你需要能够识别父类和子类,并理解继承如何支持多态。


6. Polymorphism | 多态

Polymorphism means “many forms” and allows objects of different classes to respond to the same method call in their own way. This is often implemented through method overriding, where a subclass provides a specific implementation of a method already defined in its superclass. For instance, both Circle and Rectangle subclasses of a Shape class can have a draw() method, but each draws itself differently. Polymorphism simplifies code that works with objects of a common supertype.

多态意为“多种形态”,它允许不同类的对象以各自的方式响应相同的方法调用。这通常通过方法重写来实现,即子类提供对父类中已定义方法的具体实现。例如,Shape 类的子类 CircleRectangle 都可以有 draw() 方法,但每个方法绘制自身的方式不同。多态简化了与共同父类型对象交互的代码。


7. Abstract Classes and Interfaces | 抽象类与接口

An abstract class is a class that cannot be instantiated directly and is designed to be subclassed. It may contain abstract methods (without implementation) that must be overridden by subclasses. An interface (in languages like Java) defines a contract of methods that implementing classes must fulfil. Both concepts enforce a consistent design and support polymorphism. Edexcel A-Level covers abstract classes as a way to define a common template while leaving specific behaviours to subclasses.

抽象类是不能直接实例化、专为子类化而设计的类。它可以包含必须在子类中被重写的抽象方法(没有实现)。接口(在 Java 等语言中)定义了实现类必须履行的行为契约。这两个概念都强制了一致的设计,并支持多态。Edexcel A-Level 将抽象类视为定义通用模板、而将特定行为留给子类的一种方式。


8. Constructor Methods | 构造方法

A constructor is a special method that is automatically called when an object of a class is created. It is typically used to initialise the object’s attributes. In Python, the constructor is __init__(); in Java, it is a method with the same name as the class. Constructors can be overloaded to accept different sets of parameters, providing flexibility in object creation. Understanding constructors is vital for working with OOP in any language featured in the Edexcel course.

构造方法是一种特殊方法,在创建类的对象时自动调用。它通常用于初始化对象的属性。在 Python 中,构造方法是 __init__();在 Java 中,它是一个与类同名的方法。构造方法可以重载以接受不同的参数集,从而为对象创建提供灵活性。理解构造方法对于在 Edexcel 课程所涵盖的任何语言中使用 OOP 都至关重要。


9. Advantages of OOP | 面向对象编程的优点

OOP offers several benefits: improved code reuse through inheritance, easier maintenance due to modularity, enhanced security with encapsulation, and greater flexibility via polymorphism. It also makes it simpler to map real-world problems to program structures. These advantages explain why OOP is the dominant paradigm in modern software development and why the Edexcel syllabus places significant emphasis on it.

面向对象编程提供了诸多优点:通过继承提高代码复用性,通过模块化便于维护,通过封装增强安全性,通过多态提高灵活性。它还使得将现实世界问题映射到程序结构变得更加简单。这些优点解释了为什么 OOP 成为现代软件开发中的主导范式,以及为什么 Edexcel 大纲对其给予了高度重视。


10. OOP in the Edexcel A-Level Curriculum | Edexcel A-Level 课程中的面向对象编程

The Edexcel A-Level Computer Science specification (2015) includes OOP in Topic 1.7 (Programming Paradigms) and throughout the problem-solving components. Students are expected to define and use classes, implement encapsulation, apply inheritance and polymorphism, and evaluate the suitability of OOP for given scenarios. Practical programming tasks often require designing class diagrams and writing object-oriented code in a language such as Python or Java. Mastering these concepts is not only essential for the written examination but also for the non-exam assessment (NEA) project.

Edexcel A-Level 计算机科学大纲(2015版)在主题 1.7(编程范式)以及问题解决相关部分中包含了面向对象编程。学生需要定义和使用类、实现封装、应用继承和多态,并评估 OOP 对给定场景的适用性。实践编程任务通常要求设计类图并用 Python 或 Java 等语言编写面向对象代码。掌握这些概念不仅对笔试至关重要,对非考试评估(NEA)项目也是如此。


11. Key Terminology Summary | 关键术语总结

Term English Definition 中文释义
Class Blueprint for objects 类的蓝图
Object Instance of a class 类的实例
Encapsulation Bundling data and methods, restricting access 封装数据和方法,限制访问
Inheritance Deriving a new class from an existing one 从现有类派生新类
Polymorphism Ability to take many forms; method overriding 多种形态的能力;方法重写
Abstract Class Class that cannot be instantiated, with abstract methods 无法实例化、含抽象方法的类
Constructor Special method to initialise objects 用于初始化对象的特殊方法

Published by TutorHao | Programming 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