Writing a class
After this: Write a class with a constructor and fields, and make two objects from it.
On this page · 7 sections · 3 checks
A recipe is not a cake. It says what goes into every cake of that kind and how it is made, and from one recipe you bake as many cakes as you like. In lesson 1 you used a description of an account without reading it closely. This lesson reads it line by line, and shows how the account was made.
A class and its objects
A is the description of a kind of thing: what every one of them holds, and what every one of them can do. An object is one thing made from it. Account is a class. Ana's account and Ben's account are two objects of that class.
The first line of a class is its name: class Account:class Account {. The lines pushed in under it belong to the class.
The constructor
When the program writes Account("Ana", 100)new Account("Ana", 100), a new, empty object is made, and then one special function runs to fill it: the . Its parameters are the values a new object needs, and its lines copy each one into the object.
In Python the constructor is named __init__, with two underscores on each side, and Python looks for that exact name. Its first parameter is self: the new object. The other parameters, name and balance, are filled from the call as any parameters are.In Java the constructor has the class's own name, Account, and no return type: not even void. Its parameters, name and balance, are filled from the call as any parameters are. Inside it, the word this is the new object.The line self.name = namethis.name = name; says: take the value in the parameter name and store it in the new object, under the name name. A value stored inside an object is a . Python's own word is attribute; this learning path says field for both languages. An object's fields are its data. self.namethis.name, with the dot, means "the field name of this object", and the plain name means the parameter.
Work through a = Account("Ana", 100)Account a = new Account("Ana", 100);:
- A new, empty account is made.
- The constructor is called with it. A frame opens with selfthis holding the empty object,
nameholding Ana andbalanceholding 100. - self.name = namethis.name = name; gives the object its first field. The band shows selfthis as Account(name='Ana')Account(name="Ana").
- self.balance = balancethis.balance = balance; gives it the second: Account(name='Ana', balance=100)Account(name="Ana", balance=100).
- The constructor returns nothing, the frame closes, and the object goes into the box
a.
- Run the programRun it and step through. When the program reaches the line that makes the account, a frame opens with three boxes: self (this in Java), name and balance. Watch self fill up, one field per line, and watch what the box a holds once the frame is gone.
class Account: def __init__(self, name, balance): self.name = name self.balance = balance
a = Account("Ana", 100)print(a.name)print(a.balance)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Reading a field from outside uses the same dot: a.name is Ana, and a.balance is 100.
Two objects from one class
Each call of the constructor fills a different object, from its own arguments.
class Account: def __init__(self, name, balance): self.name = name self.balance = balance
a = Account("Ana", 100)b = Account("Ben", 10)print(a.name, a.balance)print(b.name, b.balance)a.balance = 0print(a.balance, b.balance)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
a holds Ana and 100, and b holds Ben and 10. The last two lines show something else: a field can be changed from outside with the same dot. a.balance = 0 empties Ana's account, and Ben's is untouched: 0 10.
The code
class Account: def __init__(self, name, balance): self.name = name self.balance = balance
a = Account("Ana", 100)print(a.name)print(a.balance)Ana 100
class Account { String name; int balance;
Account(String name, int balance) { this.name = name; this.balance = balance; }}
public class Main { public static void main(String[] args) { Account a = new Account("Ana", 100); System.out.println(a.name); System.out.println(a.balance); }}Ana 100
Run it
Give the constructor a third parameter, city, store it as a field, and print a.city.
Check yourself
A program defines Account and then runs a = Account("Ana", 100)Account a = new Account("Ana", 100); and b = Account("Ben", 10)Account b = new Account("Ben", 10);. Which is the class, and which are objects?
Complete the second line of the constructor, so that the value in the parameter balance is stored in the new account's own field, and the program prints 100.
class Account: def __init__(self, name, balance): self.name = name ____________________ # ← put balance in the new account's own box
a = Account("Ana", 100)print(a.balance)class Account { String name; int balance;
Account(String name, int balance) { this.name = name; ____________________ // ← put balance in the new account's own box }}
public class Main { public static void main(String[] args) { Account a = new Account("Ana", 100); System.out.println(a.balance); }}Two books from one class. What does this print?
class Book: def __init__(self, title, pages): self.title = title self.pages = pages
x = Book("Dune", 412)y = Book("Emma", 300)print(y.pages)print(x.title)class Book { String title; int pages;
Book(String title, int pages) { this.title = title; this.pages = pages; }}
public class Main { public static void main(String[] args) { Book x = new Book("Dune", 412); Book y = new Book("Emma", 300); System.out.println(y.pages); System.out.println(x.title); }}Honesty notes
- Write the first line of a class, and say that a class describes a kind of thing while an object is one such thing.
- Write a constructor that takes the values for a new object and puts each in one of the object's fields.
- Read a field with a dot, as in a.balance, and change it the same way.
- Say that each object made from the class gets fields of its own, filled from the arguments of its own call.