最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
class扩展
时间:2026-06-06 08:24:12 编辑:袖梨 来源:一聚教程网
Lesson: Introduction to Python Classes & Objects
Duration: 90 Minutes | Topic: Object-Oriented Programming (OOP)

Part 1: The "Blueprint" Concept (10 mins)
Goal: Understand why we use classes.
Explanation for Student: Imagine you want to build a house. You don't just start building randomly. You need a Blueprint (a plan).
- The Class: The Blueprint. It defines what a house has (windows, doors, color).
- The Object: The actual House. You can use one blueprint to build many houses. Each house can have a different color, but they all follow the same plan.
Part 2: Syntax - Our First Class (20 mins)
Goal: Learn class, __init__, and self.
Code Example:
class Dog:
# The "Constructor" - This runs when we create a new dog
def __init__(self, name, breed, age):
self.name = name # Attribute
self.breed = breed # Attribute
self.age = age # Attribute # A Method (What the dog can DO)
def bark(self):
print(f"{self.name} says: Woof! Woof!")# --- Creating Objects ---
my_dog = Dog("Buddy", "Golden Retriever", 3)
your_dog = Dog("Lucy", "Poodle", 5)print(my_dog.name) # Output: Buddy
my_dog.bark() # Output: Buddy says: Woof! Woof!
Key Concepts for Student:
__init__: The "Starting Point." It sets up the object's information.self: Refers to "this specific object." When Buddy barks,self.nameis Buddy. When Lucy barks,self.nameis Lucy.- Attributes: Variables that belong to the object (name, age).
- Methods: Functions that belong to the object (bark, run).
Part 3: Interactive Challenge - "Design a Hero" (20 mins)
Goal: Practice creating attributes and basic methods.
Task: Create a class called Hero for an RPG game.
- Attributes:
name,health(default 100),power. - Method:
rest()-> Increases health by 10. - Method:
stats()-> Prints the hero's current status.
Solution Template (Don't show the student yet!):
class Hero:
def __init__(self, name, power):
self.name = name
self.power = power
self.health = 100 def rest(self):
self.health += 10
print(f" {self.name} is resting. Health is now {self.health}") def stats(self):
print(f" Hero: {self.name} | HP: {self.health} | Power: {self.power}")# Student should test it:
p1 = Hero("Zelda", 25)
p1.stats()
p1.rest()
Part 4: Object Interaction - "The Battle" (20 mins)
Goal: Learn how two objects interact with each other.
Explanation for Student: Objects aren't just isolated. They can interact! We can pass one object into the method of another object.
Code Extension:
class Hero:
def __init__(self, name, health, power):
self.name = name
self.health = health
self.power = power def attack(self, enemy):
print(f"️ {self.name} attacks {enemy.name}!")
enemy.health -= self.power
print(f" {enemy.name} now has {enemy.health} HP left.")# Creating two heroes
hero = Hero("Link", 100, 20)
monster = Hero("Ganon", 200, 10)# Let the interaction happen
hero.attack(monster)
Part 5: Final Boss Task - "The Bank Account" (20 mins)
Goal: Independent coding to consolidate everything.
Instructions for Student:
Create a class BankAccount.
- Attributes:
owner(string),balance(number). - Method
deposit(amount): Adds money to balance. - Method
withdraw(amount):- If balance is enough: Subtract money.
- If not enough: Print "Insufficient funds!"
- Task: Create an account for yourself with 50. Try to withdraw $200.
Student Cheat Sheet (Print or share this)
| Concept | Python Syntax | Meaning |
|---|---|---|
| Create a Class | class Name: | Define the blueprint |
| Initialize | def __init__(self, ...): | Set the starting stats |
| Identity | self | Referring to "this specific" object |
| Action | def method_name(self): | What the object can do |
| Instance | obj = Name() | Making a real thing from the blueprint |
Troubleshooting / FAQ
- Error:
TypeError: __init__() takes 3 positional arguments but 4 were given- Cause: The student forgot
selfin the__init__definition.
- Cause: The student forgot
- Error:
NameError: name 'health' is not defined- Cause: Inside a method, they wrote
healthinstead ofself.health.
- Cause: Inside a method, they wrote
- Conceptual Confusion: "Why do we need classes?"
- Answer: "If we have 100 dogs, without classes, we need 300 variables. With classes, we just need 100 objects."
相关文章
- 微信小程序开发入门指南 - 2026最新基础教程 06-13
- 速读免费小说app如何看小说赚钱 06-13
- 2026年百川智能进阶技巧:医疗场景3个常见避坑点 06-13
- 2026年讯飞星火入门避坑指南:如何避开4个常见设置误区? 06-13
- 百川智能实用插件推荐2026版:办公与开发场景下的3种选择 06-13
- 互联网核心功能详解 - 2026最新基础原理与应用 06-13