Object-Oriented and Structured Programming: A Combined Approach | 面向对象与结构化编程:结合方法

📚 Object-Oriented and Structured Programming: A Combined Approach | 面向对象与结构化编程:结合方法

In Edexcel A-Level programming, you need to master two fundamental programming paradigms: structured programming and object-oriented programming (OOP). Real-world software development rarely uses them in isolation. The real power emerges when you understand how to combine the clarity of structured control flow with the scalability of OOP. This article explores both paradigms, their key features, and practical ways to blend them in your own code.

在 Edexcel A-Level 编程中,你需要掌握两种基本的编程范型:结构化编程和面向对象编程(OOP)。现实世界的软件开发很少单独使用其中一种。当你理解如何将结构化控制流的清晰性与 OOP 的可扩展性相结合时,真正的威力就显现出来了。本文探讨这两种范型、它们的关键特性以及在自己的代码中混合使用它们的实用方法。

1. Understanding Programming Paradigms | 理解编程范型

A programming paradigm is a fundamental style of coding that determines how the structure and elements of a program are organised. Structured programming focuses on decomposing a problem into procedures and using sequence, selection, and iteration. Object-oriented programming organises code around objects that contain data and methods. The Edexcel specification requires you to compare these paradigms and apply them appropriately.

编程范型是一种基本的编码风格,它决定了程序的结构和元素如何组织。结构化编程侧重于将问题分解为过程,并使用顺序、选择和迭代。面向对象编程则将代码围绕包含数据和方法的对象进行组织。Edexcel 规范要求你比较这些范型,并恰当地应用它们。


2. The Three Pillars of Structured Programming | 结构化编程的三大支柱

Structured programming is built on three core control constructs: sequence (executing statements in step-by-step order), selection (using if-else or case structures to make decisions), and iteration (repeating blocks with for, while, or do-while loops). These constructs eliminate the need for unpredictable ‘goto’ statements and produce code that is easy to trace and debug.

结构化编程建立在三个核心控制结构上:顺序(按逐步顺序执行语句)、选择(使用 if-else 或 case 结构做出决策)和迭代(使用 for、while 或 do-while 循环重复代码块)。这些结构消除了对不可预测的 ‘goto’ 语句的需求,并生成了易于追踪和调试的代码。


3. The Four Pillars of Object-Oriented Programming | 面向对象编程的四大支柱

OOP rests on four principles: encapsulation, abstraction, inheritance, and polymorphism. Encapsulation bundles attributes and methods in a class and controls access with private/public modifiers. Abstraction hides complex implementation details. Inheritance allows child classes to reuse and extend parent behaviour. Polymorphism lets a single interface represent different underlying forms, often achieved through method overriding.

面向对象编程建立在四个原则上:封装、抽象、继承和多态。封装将属性和方法捆绑在类中,并用 private/public 修饰符控制访问。抽象隐藏复杂的实现细节。继承允许子类重用和扩展父类的行为。多态使单一接口能代表不同的底层形式,通常通过方法重写实现。


4. Structured Programming in Practice: Modules and Parameters | 实践中的结构化编程:模块与参数

In structured code, a large program is divided into functions and procedures. Each module performs a well-defined task, taking parameters as input and returning values as output. This top-down design encourages code reuse and readability. It also promotes loose coupling, as modules interact only through their interfaces rather than global variables.

在结构化代码中,大型程序被分成函数和过程。每个模块执行一个明确定义的任务,接受参数作为输入并返回值作为输出。这种自顶向下的设计鼓励代码重用和可读性。它还促进了松散耦合,因为模块仅通过接口交互,而不是通过全局变量。


5. OOP in Practice: Designing Classes and Objects | 实践中的面向对象编程:设计类与对象

Applying OOP starts with identifying real-world entities relevant to the problem. You model these as classes, defining attributes (fields) and behaviours (methods). For example, a ‘Student’ class might have fields such as studentID and name, and methods like enrolCourse(). An object is an instance of a class, holding its own state. This approach mirrors real-life systems, making complex projects easier to manage.

应用面向对象编程从识别与问题相关的现实世界实体开始。你将它们建模为类,定义属性(字段)和行为(方法)。例如,‘Student’ 类可能包含 studentID 和 name 等字段,以及 enrolCourse() 等方法。对象是类的一个实例,拥有自己的状态。这种方法反映了现实世界系统,使复杂项目更易于管理。


6. Why Combine Structured and Object-Oriented Approaches? | 为什么结合结构化与面向对象方法?

No modern application relies exclusively on one paradigm. Inside a class method, you will use structured if-else and loops to implement the logic. The class provides the encapsulation and reusability; the structured control flow ensures the method is correct and readable. Combining them gives you the micro-level clarity of structured programming and the macro-level organisation of OOP.

现代应用程序不会完全依赖单一种范型。在类方法内部,你会使用结构化的 if-else 和循环来实现逻辑。类提供了封装和可重用性;而结构化控制流确保方法正确且可读。结合二者,你就获得了结构化编程在微观层面的清晰性和面向对象编程在宏观层面的组织性。


7. Example: Blending Paradigms Inside a BankAccount Class | 示例:在 BankAccount 类中融合范型

Consider a simple BankAccount class. The deposit(amount) method needs to check that the amount is positive (selection), and perhaps apply a bonus while a counter is below a limit (iteration). The code below illustrates this combination concisely:

考虑一个简单的 BankAccount 类。deposit(amount) 方法需要检查金额是否为正(选择),并且可能在计数器低于某个限制时应用奖金(迭代)。下面的代码简洁地说明了这种结合:

class BankAccount:
    private balance ← 0
    public procedure deposit(amount)
        if amount > 0 then
            balance ← balance + amount
        else
            output “Invalid amount”
        endif
    endprocedure
endclass

虽然这是一个面向对象的类,但其方法内部完全依赖于结构化流程。这个方法可重用,并且封装的数据不会被外部直接篡改。

Although this is an object-oriented class, the inside of its method relies entirely on structured flow. The method is reusable, and the encapsulated data is not directly tampered with from outside.


8. Top-Down Design Meets Encapsulation | 自顶向下设计遇上封装

A top-down design strategy (a hallmark of structured programming) can be used to plan the classes in an OOP system. You first define high-level responsibilities, then break them into class methods, and finally implement each method using structured constructs. This layered approach keeps the overall architecture clean while ensuring low-level logic is robust.

自顶向下的设计策略(结构化编程的标志)可用于规划面向对象系统中的类。你首先定义高层职责,然后将其分解为类方法,最后使用结构化构造实现每个方法。这种分层方法保持整体架构清晰,同时确保低层逻辑健壮。


9. Inheritance and Modular Code Organisation | 继承与模块化代码组织

Inheritance allows you to create a hierarchy of classes that share common behaviour, reducing code duplication. Structured decomposition is used inside each class method. For instance, a generic ‘Vehicle’ class might declare an abstract move() method, while ‘Car’ and ‘Bike’ subclasses provide their own implementations using iteration and conditions. The paradigm combination keeps both the class tree and the method logic well-structured.

继承允许创建共享共同行为的类层次结构,从而减少代码重复。结构化分解在每个类方法内部使用。例如,一个通用的 ‘Vehicle’ 类可以声明一个抽象的 move() 方法,而 ‘Car’ 和 ‘Bike’ 子类使用迭代和条件提供自己的实现。范型的结合使类树和方法逻辑都保持结构良好。


10. Case Study: A Combined Approach in a Shopping Cart | 案例研究:购物车中的结合方法

Imagine a ShoppingCart class containing a list of items. The calculateTotal() method might iterate through the list (iteration) and for each item apply a discount if it is on sale (selection). The class itself encapsulates the item list and exposes only safe operations. This shows how a real-world feature naturally merges OOP structure with procedural logic.

想象一个 ShoppingCart 类,其中包含一个商品列表。calculateTotal() 方法可能遍历该列表(迭代),并对每个在售的商品应用折扣(选择)。类本身封装了商品列表,仅暴露安全的操作。这表明现实世界功能如何自然地将 OOP 结构与过程化逻辑融合。


11. Common Pitfalls When Mixing Paradigms | 混合范型时的常见陷阱

One mistake is using global variables inside class methods, which breaks encapsulation. Another is writing massive methods that perform too many tasks, violating both the single-responsibility principle and modular decomposition. Also, over-engineering a class hierarchy for a simple problem can make code harder to follow. Always balance: use robust OOP boundaries but keep the logic within methods clean and sequential.

一个常见的错误是在类方法内部使用全局变量,这破坏了封装性。另一个错误是编写执行过多任务的庞大方法,既违反了单一职责原则,也违反了模块化分解。此外,为简单问题过度设计类层次结构会使代码更难理解。始终保持平衡:使用健壮的 OOP 边界,但让方法内的逻辑保持清晰有序。


12. Edexcel A-Level Exam Focus | Edexcel A-Level 考试焦点

In Edexcel A-Level programming questions, you may be asked to compare the two paradigms or to write code that combines them. Examiners look for evidence that you can identify where structured loops and selections are used inside a class method. They also reward clear naming, appropriate encapsulation, and the correct application of inheritance. Practice writing short programs that define a class with at least one method that uses both an if statement and a loop, and explain why this is an example of paradigm combination.

在 Edexcel A-Level 编程试题中,你可能会被要求比较这两种范型,或编写结合它们的代码。考官期望你能识别出在类方法内部使用结构化循环和选择的地方。他们还会奖励清晰的命名、适当的封装以及对继承的正确应用。练习编写简短的程序,定义一个包含至少一个方法的类,其中同时使用了 if 语句和循环,并解释为什么这是范型结合的例子。

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