创建类与对象
构建您的第一个 Python 类。
创建类与对象 是 CoddyKit 上的免费 Python For Kids 课时。 这是第 2 节课,共 5 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Python For Kids 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Python For Kids 课程共包含 5 节课。
创建类和对象
欢迎回到面向对象编程(OOP)课程!今天,我们将学习如何在 Python 中创建自己的类和对象。类和对象通过将相关的属性和行为组合在一起,帮助您组织代码。

什么是类?
类就像是创建对象的蓝图。它定义了由该类创建的对象所具有的属性(attributes)和行为(方法)。
- attributes:对象的特征(例如颜色、大小)。
- 方法:对象可以执行的操作(例如移动、说话)。
您可以把类想象成食谱,把对象想象成根据食谱制作出的菜肴。
在 Python 中定义类
在 Python 中,您可以使用 class 关键字定义类,后面跟类名和冒号。在类的内部,您可以定义 attributes 和方法。
语法:
class ClassName:
def __init__(self, attribute1, attribute2):
self.attribute1 = attribute1
self.attribute2 = attribute2
def method_name(self):
# Code for the method
pass
__init__ 方法
__init__ 方法是 Python 类中的特殊方法。创建类的新对象时会调用它,用于初始化对象的 attributes。
示例:
# Defines the Person class with name and age attributes
class Person:
def __init__(self, name, age):
self.name = name # Sets the name of the person
self.age = age # Sets the age of the person
从类创建对象
定义类后,您可以像调用函数一样调用类名,从该类创建对象(实例)。
示例:
# Creates an object of the Person class
person1 = Person("Alice", 12)
# Accesses the object's attributes
print(person1.name) # Output: Alice
print(person1.age) # Output: 12
向类添加方法
方法是在类内部定义的函数,用于描述由该类创建的对象的行为。
示例:
# Defines the Person class with a greet method
class Person:
def __init__(self, name, age):
self.name = name # Sets the name of the person
self.age = age # Sets the age of the person
def greet(self):
print("Hello, my name is " + self.name + "!") # Prints a greeting message
# Creates an object of the Person class
person1 = Person("Bob", 15)
# Calls the greet method of the object
person1.greet() # Output: Hello, my name is Bob!
示例程序:构建一个简单的类
让我们创建一个简单的 Car 类,其中包含 attributes 和方法,以了解类和对象的工作方式。
代码:
# Defines the Car class
class Car:
def __init__(self, make, model, color):
self.make = make # Sets the make of the car
self.model = model # Sets the model of the car
self.color = color # Sets the color of the car
def display_info(self):
print("Car Make:", self.make) # Prints the make of the car
print("Car Model:", self.model) # Prints the model of the car
print("Car Color:", self.color) # Prints the color of the car
def paint(self, new_color):
self.color = new_color # Changes the color of the car
print("The car has been painted to", self.color) # Confirms the color change
# Creates an object of the Car class
my_car = Car("Toyota", "Corolla", "Red")
# Calls the display_info method
my_car.display_info()
# Output:
# Car Make: Toyota
# Car Model: Corolla
# Car Color: Red
# Calls the paint method to change the car's color
my_car.paint("Blue")
# Output: The car has been painted to Blue
# Displays the updated car information
my_car.display_info()
# Output:
# Car Make: Toyota
# Car Model: Corolla
# Car Color: Blue
实例 variable 与类 variable
在 Python 类中,variable 可以分为以下两类:
- 实例 variable:每个对象实例独有。
- 类 variable:由该类的所有实例共享。
让我们看看如何使用这两种类型的 variable。
# Defines the Student class with a class variable
class Student:
school = "XYZ High School" # Class variable
def __init__(self, name):
self.name = name # Instance variable
def display_info(self):
print(self.name + " attends " + Student.school) # Prints student info
# Creates two objects of the Student class
student1 = Student("Alice")
student2 = Student("Bob")
# Calls the display_info method for both students
student1.display_info()
student2.display_info()
做得很好!
您已经学会了如何在 Python 中创建类和对象,并通过定义 attributes 和方法有效地组织代码。理解类和对象是面向对象编程(OOP)的基础,有助于您构建更复杂且可复用的程序。请继续练习,创建自己的类并尝试使用不同的 attributes 和方法!

常见问题解答
「创建类与对象」课时是免费的吗?
是的 — 「创建类与对象」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Python For Kids 课程的其余内容,请升级到 CoddyKit PRO。 Python For Kids 课程共包含 5 节课。
「创建类与对象」这节课中我会学到什么?
构建您的第一个 Python 类。 你通过在浏览器中直接运行的动手代码来练习 Python For Kids,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Python For Kids 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Python For Kids 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 5 节。
「创建类与对象」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Python For Kids 课中编写并运行代码吗?
能。每节 Python For Kids 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。