Objects and behaviour
What is object-oriented programming?
On this page
Object-oriented programming organises work around objects with operations that callers can request. An inventory reservation can expose confirm() and release() and enforce the legal transitions between them. A useful first design question is who has enough information to refuse an invalid request.
Worked example
Pythonclass Reservation:
def __init__(self):
self.state = "held"
def confirm(self):
if self.state != "held":
raise ValueError("Only held seats can be confirmed")
self.state = "confirmed"
seat = Reservation()
seat.confirm()
assert seat.state == "confirmed"Explore class relationships in the OOP Playground