Object-Oriented Programming: Combined Concepts | 面向对象编程综合概念

📚 Object-Oriented Programming: Combined Concepts | 面向对象编程综合概念

This revision article covers the core principles of object-oriented programming (OOP) as outlined in the Edexcel A-Level Computer Science specification. From classes and objects to inheritance, encapsulation, polymorphism, and design relationships, each concept is paired with its Chinese equivalent to support bilingual learners. The material is structured to help you understand how OOP enables modular, reusable, and secure code through real-world analogies and clear technical definitions.

本文依据 Edexcel A-Level 计算机科学大纲,系统梳理了面向对象编程(OOP)的核心原理。从类与对象、封装、继承、多态到类间关系,每个概念都提供中英对照,并借助生活化类比与技术定义帮助你理解 OOP 如何构建模块化、可复用且安全的代码。

1. The Object-Oriented Paradigm | 面向对象范式

The object-oriented paradigm models software around ‘objects’ that contain both data (attributes) and behaviour (methods). Unlike procedural programming, where the focus is on sequences of instructions, OOP structures programs as interacting entities. This mirrors how we perceive the real world: a car has properties like colour and speed, and it can accelerate or brake. By grouping related data and behaviour, OOP reduces complexity and improves maintainability.

面向对象范式围绕“对象”构建软件,对象包含数据(属性)和行为(方法)。与专注于指令序列的过程式编程不同,OOP 将程序组织为彼此交互的实体。这符合我们对现实世界的认知:一辆汽车有颜色和速度等属性,并且能够加速或刹车。通过将相关数据与行为分组,OOP 降低了复杂度并提升了可维护性。


2. Classes and Objects | 类与对象

A class is a blueprint or template that defines the structure and behaviour shared by a set of objects. It specifies attributes (fields) and methods, but does not hold actual data for any single entity. An object is an instance of a class; it is a concrete realisation that holds its own attribute values. For example, the class Dog might define attributes name and breed, while objects myDog and neighbourDog have separate values for those attributes.

类是定义一组对象共享的结构和行为的蓝图或模板。它规定了属性(字段)和方法,但不为任何单一实体持有实际数据。对象是类的实例,是持有自身属性值的具体实现。例如,Dog 类可以定义属性 namebreed,而对象 myDogneighbourDog 则拥有各自不同的属性值。

Instantiation is the process of creating an object from a class. In many languages, this is done using the new keyword, which allocates memory and calls a constructor. Objects communicate by invoking each other’s methods, passing messages that request an operation or return data.

实例化是从类创建对象的过程。在许多语言中,这通过 new 关键字完成,它会分配内存并调用构造函数。对象通过调用彼此的方法来通信,传递消息以请求操作或返回数据。


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

Encapsulation bundles an object’s data and the methods that operate on that data into a single unit, while restricting direct access to the internal state. This is typically achieved using access modifiers such as private for attributes and public for methods that provide controlled interaction. The internal workings of a class can be changed without affecting external code, as long as the public interface remains consistent.

封装将对象的数据以及操作这些数据的方法捆绑为单一单元,同时限制对内部状态的直接访问。这通常通过访问修饰符实现,例如属性使用 private,而提供受控交互的方法使用 public。只要公有接口保持一致,就可以更改类的内部实现而不影响外部代码。

Data hiding is a cornerstone of secure and robust design. By preventing external code from arbitrarily modifying attributes, we ensure that an object’s state remains valid. For example, a bank account object might have a private balance attribute that can only be altered through a public deposit() or withdraw() method, which includes validation logic.

数据隐藏是安全稳健设计的基石。通过阻止外部代码随意修改属性,可以确保对象的状态始终有效。例如,银行账户对象可能有一个私有属性 balance,只能通过包含验证逻辑的公有方法 deposit()withdraw() 来改变。


4. Inheritance and Code Reusability | 继承与代码复用

Inheritance allows a new class (subclass or child) to acquire the attributes and methods of an existing class (superclass or parent). This promotes code reuse and establishes a hierarchical relationship. The child class can extend or override inherited behaviour. For instance, a Vehicle superclass might define a move() method, while its subclass Car inherits that method and could override it to add engine-specific logic.

继承允许新类(子类或孩子类)获得已有类(超类或父类)的属性和方法。这促进了代码复用并建立了层次关系。子类可以扩展或重写继承的行为。例如,Vehicle 超类可以定义 move() 方法,而其子类 Car 继承该方法并可能重写它,以增加引擎相关的逻辑。

Multiple inheritance (where a class inherits from more than one parent) is not supported directly in languages like Java, but can be imitated through interfaces. Inheritance forms an ‘is-a’ relationship: a car is a vehicle. This distinguishes it from composition, which represents a ‘has-a’ relationship.

多重继承(一个类继承自多个父类)在 Java 等语言中并不直接支持,但可通过接口间接实现。继承构成“is-a”关系:轿车是一种交通工具。这将其与表示“has-a”关系的组合区分开来。


5. Polymorphism: Many Forms | 多态:多种形态

Polymorphism, meaning ‘many forms’, enables objects of different classes to be treated as objects of a common superclass. The most practical form is method overriding, where a subclass provides a specific implementation of a method that is already defined in its parent. At runtime, the correct overridden method is invoked based on the actual object type, a mechanism known as dynamic dispatch or late binding.

多态意为“多种形态”,它使得可以将不同类的对象当作公共超类的对象来处理。最实用的形式是方法重写,即子类提供对其父类已定义方法的特定实现。在运行时,会根据实际对象类型调用正确的重写方法,这种机制称为动态分派或后期绑定。

Consider a parent class Shape with a method area(). Subclasses Circle and Rectangle each override area() with their own formulas. A single loop iterating over a list of Shape references can call area() and obtain the correct result without knowing the specific subclass.

考虑父类 Shape 有一个方法 area()。子类 CircleRectangle 各自用自己的公式重写 area()。一个遍历 Shape 引用列表的循环可以调用 area() 并获得正确结果,而无需知道具体是哪个子类。


6. Abstraction and Interfaces | 抽象与接口

Abstraction focuses on exposing only essential features of an object while hiding complex implementation details. In OOP, this is achieved through abstract classes and interfaces. An abstract class cannot be instantiated on its own and may declare abstract methods that subclasses must implement. This forces a consistent contract while allowing flexibility in how that contract is fulfilled.

抽象专注于只暴露对象的本质特征,而隐藏复杂的实现细节。在 OOP 中,这通过抽象类和接口来实现。抽象类自身不能被实例化,并且可以声明抽象方法,要求子类必须实现。这样在保持契约一致的同时,允许灵活地实现该契约。

Interfaces define a set of method signatures that a class agrees to implement. Unlike abstract classes, they contain no state (attributes) and support multiple implementations in languages such as Java. By programming to an interface rather than a concrete class, developers reduce coupling and make systems easier to extend and modify.

接口定义了一组类同意实现的方法签名。与抽象类不同,接口不包含状态(属性),并且在 Java 等语言中支持多个实现。面向接口编程而非面向具体类编程,可以降低耦合,使系统更易于扩展和修改。


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

A constructor is a special method that is called automatically when an object is created. Its primary job is to initialise the new object’s attributes to valid states. Constructors typically have the same name as the class (in languages like Java and C++) and have no return type. A class can define multiple constructors with different parameter lists, a practice known as constructor overloading.

构造方法是在创建对象时自动调用的特殊方法。其主要任务是将新对象的属性初始化为有效状态。构造方法通常与类同名(如 Java 和 C++),并且没有返回值类型。一个类可以定义多个参数列表不同的构造方法,称为构造方法重载。

A default constructor is provided by the compiler if no constructor is explicitly written; it initialises attributes to default values (such as 0 or null). Constructors can also call other constructors in the same class via this() or a parent constructor via super(), ensuring that inherited attributes are properly set up.

如果没有显式编写构造方法,编译器会提供一个默认构造方法,它将属性初始化为默认值(如 0null)。构造方法还可以通过 this() 调用同一类的其他构造方法,或通过 super() 调用父类构造方法,以确保继承的属性也被正确设置。


8. Access Modifiers | 访问修饰符

Access modifiers control the visibility and accessibility of classes, attributes, and methods. Common modifiers include public, private, and protected. A public member can be accessed from any other class. A private member is accessible only within the same class, enforcing strict encapsulation. The protected modifier allows access within the same package and by subclasses, even if they are in different packages.

访问修饰符控制类、属性和方法的可见性和可访问性。常见的修饰符包括 publicprivateprotectedpublic 成员可以从任何其他类访问。private 成员只能在同一个类内访问,严格实现了封装。protected 修饰符允许在同一个包内以及子类(即使在不同包中)访问。

By setting attributes to private and providing public getter and setter methods, developers can add validation logic or logging when values are changed. This ensures that objects never enter an inconsistent state, a key advantage of OOP over procedural approaches.

将属性设为 private 并提供 public 的 getter 和 setter 方法,开发者可以在值发生变化时添加验证逻辑或日志记录。这确保对象永远不会处于不一致的状态,是 OOP 相对于过程式方法的显著优势。


9. Relationships: Association, Aggregation, Composition | 关系:关联、聚合、组合

Objects often need to collaborate, and OOP defines several relationship types. Association is a general ‘uses-a’ relationship where one object interacts with another. For example, a Teacher object uses a Classroom object. Aggregation is a specialised form of association representing a ‘has-a’ relationship with independent lifecycles: a Department has Professor objects, but the professors can exist without the department.

对象常常需要协作,OOP 定义了多种关系类型。关联是一种通用的“uses-a”关系,表示一个对象与另一个对象交互。例如,Teacher 对象使用 Classroom 对象。聚合是关联的一种特殊形式,表示具有独立生命周期的“has-a”关系:Department 拥有 Professor 对象,但教授可以脱离部门存在。

Composition is a stronger form of aggregation where the contained object’s lifecycle depends on the container; if the whole is destroyed, its parts are destroyed as well. A House is composed of Room objects: rooms do not exist independently outside the house. These relationships are often implemented through object references and careful constructor logic.

组合是一种更强的聚合形式,被包含对象的生命周期依赖于容器;如果整体被销毁,其组成部分也随之销毁。一座 HouseRoom 对象组合而成:房间不能独立于房屋存在。这些关系通常通过对象引用和精心设计的构造逻辑来实现。


10. Method Overloading vs Overriding | 方法重载与重写

Method overloading allows a class to have multiple methods with the same name but different parameter lists (number, type, or order). The appropriate method is determined at compile time based on the arguments passed. This is a form of compile-time polymorphism. Overriding, on the other hand, occurs when a subclass provides a new implementation for an inherited method with the same signature; the decision is made at runtime, supporting dynamic polymorphism.

方法重载允许一个类拥有多个同名但参数列表(参数个数、类型或顺序)不同的方法。在编译时根据传入的参数确定调用哪个方法,这是编译时多态的一种形式。而重写发生在子类为具有相同签名的继承方法提供新实现时;调用哪个方法在运行时决定,支持动态多态。

Overriding is typically annotated with @Override in Java to assist the compiler in catching errors. The overridden method cannot have a more restrictive access modifier than the parent method. Overloaded methods can have different return types as long as the parameter lists differ.

在 Java 中通常使用 @Override 注解来标识重写,以帮助编译器捕获错误。重写的方法不能拥有比父类方法更严格的访问修饰符。重载的方法只要参数列表不同,可以拥有不同的返回类型。

Feature / 特性 Overloading / 重载 Overriding / 重写
绑定时间 / Binding time Compile-time / 编译时 Runtime / 运行时
方法签名 / Method signature Must differ / 必须不同 Must be identical / 必须相同
作用范围 / Scope Within same class / 同一类内 Between parent and child / 父子类间

11. Static vs Instance Members | 静态成员与实例成员

Members declared with the static keyword belong to the class itself rather than to any particular object. A static attribute is shared among all instances of the class; if one object changes its value, the change is visible to every other instance. Static methods can be called on the class without creating an object, which makes them useful for utility functions like mathematical operations.

使用 static 关键字声明的成员属于类本身,而非任意特定对象。静态属性在类的所有实例之间共享;如果一个对象改变了它的值,所有其他实例都会看到这个变化。静态方法可以在不创建对象的情况下通过类名调用,这使其非常适合数学运算等工具函数。

Instance members, by contrast, are tied to a specific object and maintain independent state. In memory, each object gets its own copy of instance variables, while there is only one copy of a static variable per class. Accessing a static member from an instance method is allowed, but the reverse (accessing an instance member from a static method) causes a compilation error because no this context exists.

相反,实例成员与特定对象绑定并维护独立的状态。在内存中,每个对象拥有自己的一份实例变量副本,而每个类只有一份静态变量副本。从实例方法访问静态成员是允许的,但从静态方法访问实例成员会导致编译错误,因为此时不存在 this 上下文。


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