网站首页 > 技术文章 正文
在 Python 中,一切皆对象。要创建对象,使用类。
什么是类?
一个 类 就像创建对象的蓝图。想象一个 汽车工厂。工厂(类)定义了汽车应该如何建造,但它本身并不制造具体的汽车。相反,它允许我们使用相同的设计创建多个汽车(对象)。
定义一个类
使用类关键字在 Python 中定义一个类。
示例:
class Car:
brand = "Toyota"
color = "Red"
这里,创建了一个名为Car的类,它有两个属性:品牌和颜色。
什么是对象?
一个 对象 是一个类的特定实例。如果一个类是蓝图,那么对象就是使用该蓝图实际创建的物品。
创建对象
从类中创建对象,我们只需调用类名即可。
示例:
car1 = Car()
print(car1.brand) # Output: Toyota
print(car1.color) # Output: Red
这里,car1 是 Car 类的一个对象。
The__init__方法(构造函数)
有时,我们希望在创建对象时设置值。我们通过使用 __init__ 方法来完成此操作,也称为 构造函数。
示例:
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
car1 = Car("Honda", "Blue")
car2 = Car("Ford", "Black")
print(car1.brand, car1.color) # Output: Honda Blue
print(car2.brand, car2.color) # Output: Ford Black
这里,__init__ 在创建对象时设置 品牌 和 颜色 属性。
类中的方法
一个 方法 是类内部执行特定动作的函数。
示例:
class Car:
def __init__(self, brand, color):
self.brand = brand
self.color = color
def display_info(self):
print(f"Car Brand: {self.brand}, Color: {self.color}")
car1 = Car("Tesla", "White")
car1.display_info() # Output: Car Brand: Tesla, Color: White
这里,display_info() 是一个打印汽车详细信息的函数。
理解self
- `self` 代表当前对象。
- 它允许我们访问该对象的变量和方法。
例如,在display_info()中,self.brand 指的是当前对象的brand。
类变量与实例变量
- 类的变量被类中所有对象共享。
- 实例变量对每个对象都是唯一的。
示例:
class Car:
wheels = 4 # Class variable (shared by all objects)
def __init__(self, brand, color):
self.brand = brand # Instance variable (unique to each object)
self.color = color
car1 = Car("BMW", "Black")
car2 = Car("Audi", "Silver")
print(car1.wheels, car2.wheels) # Output: 4 4
print(car1.brand, car2.brand) # Output: BMW Audi
这里,车轮是一个类变量,而品牌和颜色是实例变量。
Python 中的继承
继承使能够创建一个新的类,该类从现有类中继承属性和方法。
继承示例:
class Vehicle:
def __init__(self, brand):
self.brand = brand
def show_brand(self):
print("Brand:", self.brand)
class Car(Vehicle): # Inheriting from Vehicle
def __init__(self, brand, color):
super().__init__(brand) # Call parent class constructor
self.color = color
def show_details(self):
print("Car Brand:", self.brand, "Color:", self.color)
car1 = Car("Mercedes", "Blue")
car1.show_details() # Output: Car Brand: Mercedes Color: Blue
这里,汽车 继承自 车辆,因此它可以使用 显示品牌() 和 显示详情() 方法。
结论
- 类通过将相关数据和函数分组来帮助组织代码。
- 对象是类的实例,每个对象都有自己的属性和行为。
- 方法是一个类内部执行特定任务的功能。
- 继承允许新类重用现有类的功能。
- 上一篇: 类属性,类方法:简易的Python面向对象教程
- 下一篇: Python类继承的详细指南
猜你喜欢
- 2025-03-07 深入理解 Python 元类:概念、语法与应用场景
- 2025-03-07 一文了解 Python 元类
- 2025-03-07 一文搞懂 Python 中的类和对象
- 2025-03-07 乐高python编程入门——类及其属性和方法
- 2025-03-07 Python 私有属性 & 私有方法
- 2025-03-07 如何理解Python类中的self?
- 2025-03-07 如何写好一个 Python的高质量的类
- 2025-03-07 Python 内部类:优雅地组织代码
- 2025-03-07 python入门040:根据类创建实例
- 2025-03-07 了解 Python 类和元类
- 265℃Python短文,Python中的嵌套条件语句(六)
- 264℃python笔记:for循环嵌套。end=""的作用,图形打印
- 263℃PythonNet:实现Python与.Net代码相互调用!
- 258℃Python实现字符串小写转大写并写入文件
- 257℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 117℃原来2025是完美的平方年,一起探索六种平方的算吧
- 98℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 90℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)