In Python, Class and Object are core concepts of Object-Oriented Programming (OOP). Imagine you want to make a batch of identical toy cars: the “design blueprint” for the toy car is the “class”, and each specific toy car you build from this blueprint is an “object”. A class defines the “template” for objects, including their attributes (characteristics) and methods (behaviors); an object is a concrete “instance” created from this template, with its own independent attribute values and capable of executing the methods defined in the class.
一、定义类:创建“模板”¶
To define a class, we use the class keyword, followed by the class name (typically camelCase with an uppercase first letter, following Python naming conventions), and then the class body (an indented code block). The class body can contain attributes (variables describing object characteristics) and methods (object behaviors, essentially functions).
Key Points:¶
- Class Name: Usually uses camelCase with an uppercase first letter, e.g.,
Person,Car. - Constructor
__init__: A special method automatically called when an object is created to initialize the object’s attributes. The first parameter must beself, representing the current instance (i.e., the object itself), followed by parameters to set attribute values. - Attributes: Assigned using
self.attribute_name, e.g.,self.name = name, ensuring each object has independent attribute values.
Example: Define a Simple Person Class¶
class Person:
# Constructor: Automatically called when an instance is created to initialize attributes
def __init__(self, name, age):
self.name = name # Assign the parameter `name` to the instance's `name` attribute
self.age = age # Similarly, assign the parameter `age` to the instance's `age` attribute
# Instance Method: Object behavior (first parameter must be `self`)
def greet(self):
print(f"Hello! My name is {self.name} and I'm {self.age} years old.")
二、创建实例:生成“具体对象”¶
Once a class is defined, you can create objects (instances) using class_name(), passing parameters required by the constructor (excluding self).
Key Points:¶
- When creating an object, Python automatically calls the
__init__method, withselfpointing to the current instance. - Each object is independent, with its own attribute values that do not affect others.
Example: Create Instances of the Person Class¶
# Create the first object: Xiaoming, 18 years old
person1 = Person("小明", 18)
# Create the second object: Xiaohong, 20 years old
person2 = Person("小红", 20)
三、使用对象:访问属性和调用方法¶
After creating an object, you can access attributes using object_name.attribute_name or call methods using object_name.method_name() (no need to manually pass self; Python automatically passes it).
Example: Using Instances of the Person Class¶
# Access attributes
print(person1.name) # Output: 小明
print(person2.age) # Output: 20
# Call methods
person1.greet() # Output: Hello! My name is 小明 and I'm 18 years old.
person2.greet() # Output: Hello! My name is 小红 and I'm 20 years old.
四、类与对象的核心总结¶
- Class is the Template: A “blueprint” defining attributes and methods, allowing multiple objects to be created from the same class.
- Object is the Instance: Each object has independent attribute values (e.g.,
person1andperson2can have differentnameandage). - Methods Require
self: The first parameterselfof an instance method is automatically passed by Python as the current object; no manual parameter is needed during calls. - Separation of Attributes and Methods: Attributes are variables describing objects (e.g.,
name,age), while methods are object behaviors (e.g.,greet).
By encapsulating data (attributes) and operations (methods) into classes and objects, code becomes more modular and maintainable. Beginners should remember: Define the class (template) first, then create objects (instances), and finally use objects to access attributes and methods to master the basics of Python OOP.