![]() |
|
CHAPTER 18 — Object-Oriented Programming (Beginner Overview) - Printable Version +- The Lumin Archive (https://theluminarchive.co.uk) +-- Forum: The Lumin Archive — Core Forums (https://theluminarchive.co.uk/forumdisplay.php?fid=3) +--- Forum: Courses — Structured Learning (https://theluminarchive.co.uk/forumdisplay.php?fid=69) +---- Forum: Beginner’s Guide to Coding — Python From Zero to Skill (https://theluminarchive.co.uk/forumdisplay.php?fid=72) +---- Thread: CHAPTER 18 — Object-Oriented Programming (Beginner Overview) (/showthread.php?tid=236) |
CHAPTER 18 — Object-Oriented Programming (Beginner Overview) - Leejohnston - 11-15-2025 Chapter 18 — Object-Oriented Programming (Beginner Overview) OOP (Object-Oriented Programming) lets you organise code around “objects” — things that have properties and behaviour. At first it can feel strange, but once you understand it, everything clicks. Games, apps, websites, simulations, AI systems — almost all major programs use OOP. --- 18.1 What Is an Object? An object is a bundle of: • data (attributes) • behaviour (methods/functions) Example in real life: A **Dog** has: • attributes → name, age, colour • behaviours → bark(), eat(), sleep() Programming copies this idea. --- 18.2 Creating a Class A class is a blueprint for making objects. Code: class Dog:This creates a “Dog type”, but it has no data yet. --- 18.3 The __init__ Method This runs when you create (instantiate) an object. Code: class Dog:self refers to the specific object created. Create a dog: Code: my_dog = Dog("Rex", 4)Access attributes: Code: print(my_dog.name)--- 18.4 Adding Methods (Object Behaviours) Code: class Dog:Use it: Code: d = Dog("Buddy")Output: Code: Buddy says: Woof!--- 18.5 Multiple Objects From One Class Code: d1 = Dog("Max")Each object is separate. --- 18.6 Updating Attributes Code: d = Dog("Luna")--- 18.7 Example — Simple Character Class Code: class Character:Use it: Code: hero = Character("Astra", 100)--- 18.8 Why OOP Is Useful OOP makes your programs: • more organised • easier to expand • cleaner to maintain • great for large projects • realistic (objects model real things) --- 18.9 Inheritance (Beginner Peek) A class can reuse and extend another class. Code: class Animal:Cat automatically has everything Animal has — but can also override behaviour. --- 18.10 Mini Project — Bank Account Class Your class must include: • __init__(owner, balance) • deposit(amount) • withdraw(amount) • show_balance() Example: Code: Account: Lee--- 18.11 Challenge — RPG Fighter Class Create a class with: • name • health • attack_power Functions: • attack(other_character) • heal(amount) • status() Example battle: Code: Astra attacks Vorn for 12 damage!BONUS: Add critical hits (random 1.5× damage). --- 18.12 Chapter Summary • a class is a blueprint • objects are created from that blueprint • attributes = data • methods = behaviour • __init__ runs automatically • self refers to the current object • OOP creates bigger, cleaner, scalable programs Next: Chapter 19 — Practical Project: Building a Mini Python Game This chapter turns everything learned so far into a small but exciting playable project. --- Written and Compiled by Lee Johnston — Founder of The Lumin Archive |