Programmation orientée objet
| English | Français |
|---|---|
| object/ˈɒbdʒekt/ | objet |
| encapsulation/ɪnˌkæpsjʊˈleɪʃn/ | encapsulation |
| class/klæs/ | classe |
| instance/ˈɪnstəns/ | instance |
| constructor/kənˈstrʌktə/ | constructeur |
| inheritance/ɪnˈherɪtəns/ | héritage |
| subclass/ˈsʌbklæs/ | sous-classe |
| superclass/ˈsuːpəklæs/ | superclasse |
| polymorphism/ˈpɒlɪmɔːfɪzəm/ | polymorphisme |
| abstraction/əbˈstrækʃn/ | abstraction |
| containment/kənˈteɪnmənt/ | contenancement |
| aggregation/ˌæɡrɪˈɡeɪʃn/ | agrégation |
A change in one place that used to mean a change in fifty
- A program stores a customer's balance as a public number. Twenty routines read it and fifteen write it. Now the balance must never go negative.
- Without objects, that rule has to be added at fifteen places, and the day someone adds a sixteenth writer it is broken again.
- With objects, the balance is private and the only way to change it is a method that enforces the rule. There is one place to add it and no way round it.
- That is encapsulation 封装, and it is the reason object-oriented programming exists. This lesson is objects, classes 类, and the four pillars.
Objects and classes
- An object 对象 combines attributes, its data, with methods, the operations that act on that data. It is a unit of state and behaviour together.
- A class is the blueprint: it defines what attributes and methods every object of that kind will have. An object is an instance 实例 of a class, with its own values.
- One
Taxiclass, and each actual taxi is an object with its own registration and capacity.

Attributes above, methods below, and their visibility marked
Laboratoire de concepts de programmation
Reliez les exemples au concept de programmation qu'ils illustrent.
Un objet combine :
Un objet regroupe ses données (attributs) avec les opérations (méthodes) qui agissent dessus.
Quelle est la relation entre une classe et un objet ?
Une classe Taxi définit les attributs et méthodes ; chaque taxi réel est un objet avec sa propre capacité et immatriculation.
Constructors, getters and setters
- A constructor 构造函数 is a special method that runs when an object is created, to set up its attributes to valid initial values.
- Because attributes are private, a class provides get methods to read a value and set methods to change one. A set method is where a rule such as "never negative" is enforced.
- That is what makes the private attribute worth having: the rule is enforced on every change, because there is no other route to the data.
CLASS Taxi
PRIVATE Capacity : INTEGER
PUBLIC PROCEDURE NEW(StartCapacity : INTEGER)
Capacity ← StartCapacity
ENDPROCEDURE
PUBLIC FUNCTION GetCapacity() RETURNS INTEGER
RETURN Capacity
ENDFUNCTION
ENDCLASS
Un constructeur est une méthode spéciale qui :
Le constructeur initialise les attributs d'un nouvel objet au moment de sa création.
Pillar 1: encapsulation
- Encapsulation means an object's data is hidden behind its methods. Attributes are private; outside code uses the public methods.
- Two benefits, and the exam wants both: the object can enforce its own rules on every change, and the internal representation can be changed safely later, because no outside code depends on it.
- Store the balance in pounds today and in pennies tomorrow: every user of the class still calls
GetBalance()and nothing outside breaks.
L'encapsulation signifie :
L'encapsulation masque les données ; le code externe utilise les méthodes publiques, permettant aux détails internes de changer en toute sécurité.
Que procure l'encapsulation ? Sélectionnez toutes celles qui s'appliquent.
L'encapsulation concerne la maintenabilité et la correction, pas la vitesse. Les données privées avec des méthodes publiques rendent cela possible.
Pillar 2: inheritance
- Inheritance 继承 lets a subclass 子类 be defined in terms of a superclass 父类, taking all its attributes and methods and then adding its own or overriding them.
- It models an "is-a" relationship: a Manager is an Employee, so
Managerinherits fromEmployeeand adds a department. - The benefit is that shared code is written once in the superclass, and a change to it reaches every subclass.

The shared part lives above; the differences live below
L'héritage modélise une relation « est-un » (un Manager est un Employé), donc une sous-classe hérite et étend sa super-classe.
L'héritage est une spécialisation « est-un » ; les relations « a-avoir » sont modélisées par composition.
L'héritage modélise une relation « ____ un », comme un Manager étant un Employé.
Une voiture a un moteur, c'est une composition plutôt qu'un héritage. Bien distinguer ces concepts est ce que la question teste.
Pillars 3 and 4: polymorphism and abstraction
- Polymorphism 多态 means different objects respond to the same method call in their own way. Every
Shapehas anArea()method;CircleandRectangleeach implement it differently. - Its value: code can loop over a list of shapes calling
Area()on each, without knowing or caring which kind each one is. Add aTriangleclass later and that loop needs no change at all. - Abstraction 抽象 means showing a simple interface and hiding the complexity behind it: the caller of
Area()need not know any geometry.
Le polymorphisme permet :
Le même appel (ex. Area()) se comporte correctement pour chaque type — l'appelant ne doit pas connaître la classe exacte.
Rangez les conséquences du polymorphisme par ordre, de la définition au bénéfice.
Un appel, plusieurs implémentations, et le code qui l'utilise n'a jamais besoin de connaître les nouvelles.
Worked example: name the pillar
- A
BankAccountclass keeps its balance private and providesDeposit()andWithdraw()methods that refuse to make it negative. Encapsulation: the data is hidden and the rule is enforced by the only routes to it. - A
SavingsAccountclass is defined as aBankAccountwith an added interest rate. Inheritance: an is-a relationship, reusing the superclass's code. - A loop calls
Print()on every document in a list, and each type prints itself differently. Polymorphism: one call, several implementations. - Name the pillar and the feature of the scenario that shows it.
Faites correspondre chaque pilier POO à sa signification.
Encapsulation = masquage ; héritage = spécialisation « est-un » ; polymorphisme = même interface, comportement varié.
Worked example: why OOP for a large system
- Give two advantages of using object-oriented programming for a large system. [4]
- Encapsulation means each class's internals can be changed without affecting the rest of the program, so the system is easier to maintain and a team can work on classes in parallel.
- Inheritance means shared behaviour is written once and reused, so there is less duplicated code and a fix applies everywhere at once.
- Polymorphism means new kinds can be added without changing the code that uses them. Each answer is a feature and its consequence.
A team has players; it is not a player
- Containment 包含 means an object holds another object as one of its attributes. In aggregation 聚合, the contained object can exist independently: a
Teamholds references toPlayerobjects that can move to another team. - Choose by the relationship: a
Goalkeeperis aPlayer, so use inheritance; aTeamhas players, so use aggregation. A team inheriting a player'sKick()method is a design error, even if the team would enjoy taking a penalty.
Une Équipe stocke des références vers des objets Joueur. Les joueurs subsistent lorsque l'équipe est supprimée. Quelle relation correspond ?
L'équipe contient des objets Joueur indépendants. L'héritage exigerait une relation de type « est-un ».
Worked example: design a team without copying its players
- Known: a
Playerhas a private name and aGetName()method. DesignTeamwith a private collectionMemberscontaining references toPlayerobjects and anAddPlayer(NewPlayer)method that adds a reference. - A
Playercan exist before the team, and a team can be removed while its players remain. This is aggregation. Changing a player's name through the player's own method is visible to every object holding that player; a copied name string would become stale.
Player object: name = "Sam"
Team object: Members = [reference to Sam's Player object]
Goalkeeper class: inherits the Player attributes and methods
Dans cette conception, supprimer une Équipe doit également détruire tous les Joueurs qu'elle contient.
Les Joueurs existent indépendamment. Supprimer l'équipe supprime ses références, mais pas nécessairement les objets Joueur.
Associez chaque conception à sa relation.
L'héritage modélise le « est-un » ; l'agrégation modélise le « a-un » avec des objets indépendants ; l'encapsulation contrôle l'accès aux données.
Marks that slip away
- A class is the blueprint, an object is an instance of it. Do not use the words interchangeably.
- Encapsulation is hiding data behind methods, and its benefits are rule enforcement and safe internal change. "It is more secure" is too vague.
- Inheritance is an is-a relationship. A car has a engine, which is composition, not inheritance.
- Polymorphism is the same call behaving differently per class, which is what lets a new subclass be added without changing existing code.
You've got it
- an object combines attributes and methods; a class is the blueprint and an object is an instance, set up by a constructor
- encapsulation: private data behind public methods, so rules are enforced on every change and internals can change safely
- inheritance: a subclass takes a superclass's members and adds or overrides them, modelling is-a and writing shared code once
- polymorphism: the same call implemented differently per class, so new kinds need no change to existing code; abstraction: a simple interface hiding the complexity