📚 Object-Oriented Programming: Classes, Objects and Inheritance | 面向对象编程:类、对象与继承
Object-oriented programming (OOP) is a paradigm that models real-world entities as objects containing both data and behaviour. For Edexcel A-Level Computer Science, you need to understand how classes define templates for objects, and how encapsulation, inheritance and polymorphism make code more reusable, maintainable and secure.
面向对象编程(OOP)是一种将现实世界实体建模为既包含数据又包含行为的对象的编程范式。对 Edexcel A-Level 计算机科学而言,你需要理解类如何定义对象的模板,以及封装、继承和多态如何使代码更具可重用性、可维护性和安全性。
1. What is Object-Oriented Programming? | 什么是面向对象编程?
OOP organises code around objects rather than functions and logic. Each object bundles state (fields) and behaviour (methods), which mirrors how we think about real-world things such as students, orders or bank accounts.
OOP 围绕对象而不是函数与逻辑来组织代码。每个对象将状态(字段)和行为(方法)捆绑在一起,这符合我们对现实事物(如学生、订单或银行账户)的思考方式。
A key benefit is that changes to one object type are less likely to cause unexpected side effects elsewhere, because the internal state is protected by the object’s own methods.
一个关键好处是,对某一种对象类型的修改不太可能在其他地方引起意外的副作用,因为内部状态受对象自身方法的保护。
| Procedural approach | Functions + shared data |
| OOP approach | Objects with encapsulated state + methods |
2. Classes and Objects | 类与对象
A class is a blueprint or template that defines the attributes and methods common to all objects of that type. An object is a specific instance created from a class at runtime.
类是定义某一类型所有对象共有的属性和方法的蓝图或模板。对象是在运行时由类创建的具体实例。
For example, a Student class may define fields such as name and age, and methods such as enrol(). Each individual student is then an object of that class.
例如,Student 类可以定义 name 和 age 等字段,以及 enrol() 等方法。每个具体学生就是该类的一个对象。
Student s1 = new Student(“Alex”, 17)
3. Attributes and Methods | 属性与方法
Attributes describe the state of an object, while methods define the behaviours an object can perform. In many languages, attributes are implemented as fields or properties, and methods as functions defined inside the class.
属性描述对象的状态,方法定义对象可以执行的行为。在许多语言中,属性实现为字段或属性(property),方法实现为类内部定义的函数。
Well-designed classes keep related attributes and methods together, which is sometimes called cohesion. A class with high cohesion has one clear purpose and is easier to test and reuse.
设计良好的类将相关属性和方法放在一起,这有时称为内聚性。具有高内聚性的类只有一个明确目的,因此更易于测试和重用。
- Attribute example: callDuration, accountBalance, studentName
- Method example: calculateInterest(), displayDetails(), enrolCourse()
- 属性示例:callDuration、accountBalance、studentName
- 方法示例:calculateInterest()、displayDetails()、enrolCourse()
4. Encapsulation and Visibility | 封装与可见性
Encapsulation means hiding the internal state of an object and only allowing access through public methods. This protects data from invalid changes and reduces coupling between classes.
封装意味着隐藏对象的内部状态,只允许通过公共方法进行访问。这可以保护数据免受无效修改,并降低类之间的耦合。
Access modifiers such as private, public and protected control visibility. A common pattern is to make fields private and provide public getter and setter methods that validate input before changing state.
private、public 和 protected 等访问修饰符控制可见性。常见模式是将字段设为 private,并提供 public 的 getter 和 setter 方法,在更改状态前先验证输入。
| private | Only accessible inside the same class |
| public | Accessible from any class |
| protected | Accessible in the class and its subclasses |
5. Constructors and Instantiation | 构造函数与实例化
A constructor is a special method that runs automatically when a new object is created. It usually gives initial values to attributes and ensures the object starts in a valid state.
构造函数是一种特殊方法,在创建新对象时自动运行。它通常为属性赋初值,并确保对象一开始就处于有效状态。
Constructors often have the same name as the class and may be overloaded, meaning a class can provide several constructors with different parameter lists. If no constructor is written, many languages supply a default constructor with no arguments.
构造函数通常与类同名,并且可以重载,即一个类可以提供多个具有不同参数列表的构造函数。如果没有编写构造函数,许多语言会提供一个无参数的默认构造函数。
public Student(String n, int a) { name = n; age = a; }
6. Inheritance and Superclasses | 继承与超类
Inheritance allows a child class to reuse, extend or modify the attributes and methods of a parent class. The child is called a subclass, and the parent is called a superclass.
继承允许子类重用、扩展或修改父类的属性和方法。子类称为 subclass,父类称为 superclass。
For example, a PartTimeStudent class can inherit from Student and add a field for weekly hours. This avoids duplicating shared code and supports the is-a relationship: a part-time student is a student.
例如,PartTimeStudent 类可以继承 Student 类,并增加每周课时字段。这避免了重复共享代码,并支持 is-a 关系:兼职学生是一名学生。
- Superclass: Student
- Subclass: PartTimeStudent
- Inherited members: name, age, enrol()
- New members: weeklyHours, calculateLoad()
- 超类:Student
- 子类:PartTimeStudent
- 继承成员:name、age、enrol()
- 新增成员:weeklyHours、calculateLoad()
7. Polymorphism and Method Overriding | 多态与方法重写
Polymorphism means many forms. In OOP it lets one interface be used for objects of different classes, so the correct method is chosen at runtime based on the actual object type.
多态意味着多种形式。在 OOP 中,它允许一个接口用于不同类的对象,因此在运行时根据实际对象类型选择正确的方法。
Method overriding occurs when a subclass provides its own version of a method already defined in the superclass. This lets a PartTimeStudent override calculateFees() while a FullTimeStudent provides a different implementation.
方法重写发生在子类为超类中已定义的方法提供自己的版本时。这样可以让 PartTimeStudent 重写 calculateFees(),而 FullTimeStudent 提供不同的实现。
Overloading is sometimes confused with overriding: overloading means multiple methods with the same name but different parameter lists within the same class, whereas overriding replaces an inherited method with the same signature.
重载有时与重写混淆:重载是指同一个类中方法名相同但参数列表不同的多个方法,而重写是用相同签名替换继承的方法。
8. Abstract Classes and Interfaces | 抽象类与接口
An abstract class is a class that cannot be instantiated and may contain abstract methods without implementation. Subclasses must implement these abstract methods, which enforces a consistent design.
抽象类是无法实例化的类,可能包含没有实现的抽象方法。子类必须实现这些抽象方法,从而强制实现一致的设计。
An interface is similar but contains only method signatures and constants in many languages. A class can implement multiple interfaces, which is useful because many languages only allow single class inheritance.
接口类似,但在许多语言中只包含方法签名和常量。一个类可以实现多个接口,这很有用,因为许多语言只允许单类继承。
- Abstract class: can contain state and method bodies
- Interface: specifies what a class must do, without saying how
- 抽象类:可以包含状态和方法体
- 接口:规定类必须做什么,而不规定如何做
9. Association, Aggregation and Composition | 关联、聚合与组合
Objects often work together. Association is a general relationship where one object uses or knows about another object. Aggregation and composition are stronger forms that represent whole-part relationships.
对象经常协同工作。关联是一种一般关系,表示一个对象使用或了解另一个对象。聚合和组合是表示整体-部分关系的更强形式。
Aggregation means the part can exist independently of the whole, such as a department containing lecturers. Composition means the part cannot exist without the whole, such as a house containing rooms: if the house is destroyed, the rooms are destroyed too.
聚合意味着部分可以独立于整体存在,例如一个系包含讲师。组合意味着部分不能没有整体而存在,例如房子包含房间:如果房子被摧毁,房间也就被摧毁了。
| Association | Uses or knows about another object |
| Aggregation | Part can exist independently of the whole |
| Composition | Part cannot exist without the whole |
10. Advantages and Exam Tips | 优势与考试提示
OOP supports code reuse through inheritance, improves maintainability through encapsulation, and makes large systems easier to model because classes correspond to real-world concepts. Debugging is often easier because data and the code that changes it are kept together.
OOP 通过继承支持代码重用,通过封装提高可维护性,并且由于类与现实世界概念对应,使大型系统更易于建模。调试通常也更容易,因为数据和修改数据的代码被放在一起。
In Edexcel exam questions, be precise with terminology: a class is a template, an object is an instance. Use the correct terms superclass and subclass, not parent and child unless the question does. When asked to write a class diagram, show attributes, methods and visibility markers clearly.
在 Edexcel 考试题中,术语要准确:类是模板,对象是实例。使用正确的术语 superclass 和 subclass,除非题目本身使用 parent 和 child。当要求绘制类图时,请清楚地标出属性、方法和可见性标记。
- Identify classes from a scenario: nouns often suggest classes, verbs suggest methods
- Show encapsulation by making fields private and using public getters/setters
- Explain inheritance only where an ‘is-a’ relationship truly exists
- Use method overriding to demonstrate polymorphic behaviour
- 从场景中识别类:名词通常提示类,动词通常提示方法
- 通过将字段设为 private 并使用 public getter/setter 来体现封装
- 仅在真正存在 ‘is-a’ 关系时才使用继承
- 通过方法重写展示多态行为
Published by TutorHao | Programming Revision Series | aleveler.com
更多咨询请联系16621398022(同微信)
屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导