📚 Object-Oriented Programming Concepts | 面向对象编程概念
Object-oriented programming (OOP) is a paradigm that organises software design around data, or objects, rather than functions and logic. In the Edexcel A Level Computer Science course, understanding OOP is crucial for developing robust, maintainable applications. This revision note covers key concepts including encapsulation, inheritance, polymorphism, and abstraction, providing clear definitions and practical examples aligned with the specification.
面向对象编程(OOP)是一种以数据或对象为中心组织软件设计的范式,而非围绕功能和逻辑。在Edexcel A Level计算机科学课程中,理解OOP对于开发健壮、可维护的应用程序至关重要。本复习笔记涵盖封装、继承、多态和抽象等关键概念,并提供与大纲相一致的清晰定义和实用示例。
1. Introduction to OOP | 面向对象编程简介
OOP models real-world entities as objects that have state (attributes) and behavior (methods). Unlike procedural programming, which emphasises sequences of instructions, OOP focuses on creating reusable components that interact with each other.
OOP将现实世界实体建模为具有状态(属性)和行为(方法)的对象。与强调指令序列的过程式编程不同,OOP侧重于创建可重用的、相互交互的组件。
The four main principles of OOP are encapsulation, inheritance, polymorphism, and abstraction. These principles help to reduce complexity, increase code reusability, and improve security.
OOP的四个主要原则是封装、继承、多态和抽象。这些原则有助于降低复杂性、提高代码可重用性和增强安全性。
2. Classes and Objects | 类与对象
A class is a blueprint or template that defines the attributes and methods common to a group of objects. It specifies what data the objects will hold and what operations they can perform.
类是定义一组对象共有属性和方法的蓝图或模板。它指定了对象将持有何种数据以及能够执行哪些操作。
An object is an instance of a class. When a class is defined, no memory is allocated until an object is created from it. Each object has its own unique state but shares the structure defined by the class.
对象是类的实例。定义类时不会分配内存,直到从该类创建对象。每个对象都有自己独特的状态,但共享类定义的结构。
3. Attributes and Methods | 属性与方法
Attributes (also called properties or fields) represent the data stored within an object. For example, a Car class might have attributes like colour, model, and speed.
属性(也称为特性或字段)表示对象内部存储的数据。例如,一个Car类可能有颜色、型号和速度等属性。
Methods define the behaviours that an object can perform. They are functions defined inside a class and can manipulate the object’s attributes. A Car class could have methods such as accelerate() or brake().
方法定义对象可执行的行为。它们是类内部定义的函数,可以操作对象的属性。一个Car类可能有accelerate()或brake()等方法。
4. Encapsulation | 封装
Encapsulation is the practice of hiding an object’s internal state and requiring all interaction to be performed through well-defined methods. It promotes data integrity and reduces unintended interference.
封装是隐藏对象内部状态并要求通过明确定义的方法进行所有交互的实践。它促进了数据完整性并减少了意外干扰。
In programming, encapsulation is typically achieved using access modifiers such as private and public. Attributes are often declared private while getter and setter methods provide controlled access.
在编程中,封装通常使用私有和公共等访问修饰符来实现。属性通常声明为私有的,而getter和setter方法提供受控访问。
5. Access Modifiers | 访问修饰符
Access modifiers determine the visibility of class members. Common modifiers include public, private, and protected. Public members are accessible from anywhere, private members are only accessible within the class itself, and protected members are accessible within the class and its subclasses.
访问修饰符决定了类成员的可见性。常见的修饰符包括public、private和protected。公共成员可以从任何地方访问,私有成员只能在类内部访问,受保护成员可以在类及其子类中访问。
Proper use of access modifiers is essential for encapsulation. By making attributes private, we can ensure that external code cannot directly modify the data in unexpected ways.
正确使用访问修饰符对于封装至关重要。通过将属性设为私有,我们可以确保外部代码无法以意外的方式直接修改数据。
6. Constructors | 构造函数
A constructor is a special method that is automatically called when an object is instantiated. It usually initialises the attributes of the object. The constructor has the same name as the class and no return type.
构造函数是一种特殊的方法,在实例化对象时自动调用。它通常初始化对象的属性。构造函数与类同名,且没有返回类型。
Many languages support multiple constructors through overloading. A default constructor takes no parameters, while parameterised constructors allow setting initial values at creation time.
许多语言通过重载支持多个构造函数。默认构造函数不带参数,而参数化构造函数允许在创建时设置初始值。
7. Inheritance | 继承
Inheritance allows a new class (subclass) to derive properties and behaviours from an existing class (superclass). It promotes code reuse and establishes a hierarchical relationship.
继承允许新类(子类)从现有类(超类)派生属性和行为。它促进了代码重用并建立了层次关系。
For example, a Vehicle superclass might define general attributes like speed and manufacturer. A Car subclass can inherit these and add specific attributes such as numberOfDoors.
例如,一个Vehicle超类可以定义速度和制造商等一般属性。Car子类可以继承这些属性,并添加特定属性如numberOfDoors。
In Edexcel A Level, you are expected to understand ‘is-a’ relationships, where a subclass object is also a type of the superclass. The subclass can override inherited methods to provide specialised functionality.
在Edexcel A Level中,你需要理解“is-a”关系,即子类对象也是超类的一种类型。子类可以重写继承的方法以提供专门的功能。
8. Polymorphism | 多态
Polymorphism literally means ‘many forms’. In OOP, it allows objects of different classes to be treated as objects of a common superclass, enabling a single interface to represent different underlying forms.
多态字面意思是“多种形态”。在OOP中,它允许将不同类的对象视为共同超类的对象,使一个接口可以代表不同的底层形式。
Method overriding is a key technique for achieving polymorphism. A subclass can provide its own implementation of a method that is already defined in the superclass. The correct method is chosen at runtime based on the actual object type, a process known as dynamic binding.
方法重写是实现多态的关键技术。子类可以提供超类中已定义方法的自己的实现。正确的方法在运行时根据实际对象类型选择,这一过程称为动态绑定。
Polymorphism promotes flexibility and extensibility. You can write code that works with superclass references, and it will automatically invoke the appropriate subclass behaviour without modification.
多态提高了灵活性和可扩展性。你可以编写使用超类引用的代码,它将在不修改的情况下自动调用适当的子类行为。
9. Overriding and Overloading | 方法重写与重载
Overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method signature (name and parameters) must be the same.
重写发生在子类提供其超类中已定义方法的特定实现时。方法签名(名称和参数)必须相同。
Overloading, on the other hand, refers to defining multiple methods with the same name but different parameter lists within the same class. This is not directly related to inheritance but is a form of compile-time polymorphism.
另一方面,重载指的是在同一个类中定义多个同名但参数列表不同的方法。这与继承没有直接关系,但属于编译时多态的一种形式。
| Feature | Overriding | Overloading |
|---|---|---|
| Definition | Providing a new implementation for an inherited method | Multiple methods with the same name but different parameters |
| Inheritance | Required (between subclass and superclass) | Not required (can be within the same class) |
| Method Signature | Same name, same parameters | Same name, different parameters |
| Polymorphism Type | Runtime (dynamic) polymorphism | Compile-time (static) polymorphism |
Understanding the distinction is critical for designing flexible class hierarchies in OOP. Overriding enables dynamic behaviour based on the actual object, while overloading offers convenience by allowing methods to handle different types or numbers of arguments.
理解这一区别对于设计灵活的类层次结构至关重要。重写可根据实际对象启用动态行为,而重载通过允许方法处理不同类型或数量的参数提供便利。
10. Abstract Classes | 抽象类
An abstract class is a class that cannot be instantiated on its own and is designed to be a base class for other classes. It may contain abstract methods—methods without a body that must be implemented by non-abstract subclasses.
抽象类是不能独立实例化的类,旨在作为其他类的基类。它可以包含抽象方法——没有方法体的方法,这些方法必须由非抽象子类实现。
For instance, an abstract class Shape might declare an abstract method calculateArea(). Concrete subclasses like Circle and Rectangle must provide their own implementations. This enforces a common interface while allowing different behaviours.
例如,一个抽象类Shape可以声明一个抽象方法calculateArea()。像Circle和Rectangle这样的具体子类必须提供自己的实现。这在允许不同行为的同时强制规定了公共接口。
Abstract classes are useful for defining a template for a group of related classes. They support polymorphism and prevent direct creation of generic objects that lack specific functionality.
抽象类可用于定义一组相关类的模板。它们支持多态,并防止直接创建缺乏特定功能的泛化对象。
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课程辅导,国外大学本科硕士研究生博士课程论文辅导