14.3 Classes and objects
- Approved by
- Panaversity ·
In everyday life. A builder works from a blueprint for a house. The blueprint is not a house. Each house built from it is one real thing, with its own address and paint color. All have the same rooms.
What you will learn. How to read a class: the data each object holds, and the job of each method.
Why it matters. Much of the code an agent writes, and most code a company publishes for its service, is built this way.
What is a class, and what is an object?
A blueprint, and one thing built from it.
See a small one work. This class is the blueprint for one invoice. Two objects are built from it, as two houses are built from one blueprint, and each holds its own number and amount. The comments name the parts. Predict both lines.
class Invoice {
// its two fields
number: string;
cents: number;
// the constructor: it runs
// when an object is built,
// and fills in its fields
constructor(number: string, cents: number) {
this.number = number;
this.cents = cents;
}
// a method: "this" is the
// object it was called on
needsSecondApprover(): boolean {
// is this one's amount
// over $10,000.00?
return this.cents > 1000000;
}
}
// build one object
const first = new Invoice("INV-8301", 482500);
// and another, with its own data
const second = new Invoice("INV-8303", 1290000);
console.log(first.number, first.needsSecondApprover());
console.log(second.number, second.needsSecondApprover());INV-8301 false
INV-8303 trueEach object holds its own cents, so the same method gives two different answers. These words name the parts.
| Part | What it is | In the code above |
|---|---|---|
| Class | A blueprint for a kind of thing | Invoice |
| Object, or instance | One thing built from it | first and second |
| Fields | The data each object holds | number, cents |
| Constructor | The method that runs when an object is built and fills in its fields | Takes a number and an amount |
| Methods | Functions that work on that object's data | needsSecondApprover() |
Inside a method, the object is this in TypeScript and self in Python.
How do I read a class I did not write?
By its fields, and by the one-sentence job of each method.
At work. The agent reorganized Brightline's scripts into two small classes. VendorDirectory looks up vendor names by id. InvoiceBook holds the invoices and a directory, and answers questions about them. Ana lists each class's fields, and says what each method does in one sentence.
Read a short version of VendorDirectory the same way: one field, and one method. The method's last line uses the or default from 13.5, and puts a value inside text, as its comment says. The mark at the start of the field's name, # in TypeScript and _ in Python, says the field is private: it is for the class's own methods only. Predict both lines.
class VendorDirectory {
// its one field: the names,
// kept by id
#byId: Record<string, string>;
constructor(byId: Record<string, string>) {
this.#byId = byId;
}
// its one method: the name
// under this id, or this text
// if there is none.
// `...${id}` is text that
// holds a value
name(id: string): string {
return this.#byId[id] || `Unknown vendor ${id}`;
}
}
const directory = new VendorDirectory({ V005: "Hocking Valley Pallets" });
console.log(directory.name("V005"));
console.log(directory.name("V099"));Both print:
Hocking Valley Pallets
Unknown vendor V099Hold another object, or extend it?
Usually hold it. With composition, a class holds another object as a field and uses it. With inheritance, a class extends another and becomes a kind of it, taking all its methods. An InvoiceBook is not a kind of vendor directory. It has one. When an agent's class extends another, ask whether "is a kind of" is really true.
Here is a short version of InvoiceBook, with the directory from the block above. Predict what it prints.
class InvoiceBook {
// it has a directory
vendors: VendorDirectory;
constructor(vendors: VendorDirectory) {
this.vendors = vendors;
}
}
const book = new InvoiceBook(directory);
// the book asks its directory
console.log(book.vendors.name("V005"));Both print:
Hocking Valley PalletsExtending would be written class InvoiceBook extends VendorDirectory in TypeScript, or class InvoiceBook(VendorDirectory) in Python. That line would claim a book is a kind of directory.
Try it
The file in 14-3 is the small Invoice class. Run it and investigate, as in 13.1.
Modify. Send: "Change the second invoice's amount to exactly 1000000 cents. Don't run it yet." Say what each line will print. Then ask the agent to run it.
Make. Send: "Add a status field to the class and a method that answers whether the invoice is open. Print the answer for one open invoice and one paid invoice. Don't run it yet." Find the new field and the new method. Say what it will print, and then ask the agent to run it.
What you should see. After your change, both invoices answer false. Exactly $10,000.00 is not over $10,000.00.
The operating rule: read a class by its fields and the one-sentence job of each method. Prefer an object that holds another to one that extends it.

Figure 14.4. Composition. The book has a directory and asks it for names. It is not a kind of directory.
Next. In 14.4, you decide what your program lets in.
Check yourself
Question 1 / 4
0 answered
What does a constructor do?
Sources
- Owner
- Panaversity