惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
L
LangChain Blog
U
Unit 42
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
Martin Fowler
Martin Fowler
T
Tenable Blog
IT之家
IT之家
H
Help Net Security
阮一峰的网络日志
阮一峰的网络日志
Forbes - Security
Forbes - Security
N
Netflix TechBlog - Medium
The Hacker News
The Hacker News
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
美团技术团队
NISL@THU
NISL@THU
T
Threatpost
GbyAI
GbyAI
T
Threat Research - Cisco Blogs
I
InfoQ
Jina AI
Jina AI
Microsoft Azure Blog
Microsoft Azure Blog
人人都是产品经理
人人都是产品经理
C
Check Point Blog
V
Vulnerabilities – Threatpost
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Spread Privacy
Spread Privacy
S
Securelist
大猫的无限游戏
大猫的无限游戏
P
Privacy & Cybersecurity Law Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
爱范儿
爱范儿
小众软件
小众软件
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
Engineering at Meta
Engineering at Meta
S
Security Affairs
S
Secure Thoughts
T
Troy Hunt's Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
PCI Perspectives
PCI Perspectives
C
Cisco Blogs
MongoDB | Blog
MongoDB | Blog
Vercel News
Vercel News
月光博客
月光博客
Recorded Future
Recorded Future

博客园 - emanlee

ubuntu 设置北京日期时间 mysql 在新电脑上导入,出现错误:ERROR at line 4019: Unknown command '\''. - emanlee 安装 yarn npm : 无法加载文件 C:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本。 - emanlee VS code, 最近打开过哪些项目或文件夹,把这些备份下来,到另外一台电脑上恢复 傲梅分区助手 【好用】 ubuntu 可以使用Windows远程桌面连接吗?【可以】 把xshell的全部连接和密码,从一台Windows 10电脑,迁移到Windows11电脑。 Windows 远程桌面(mstsc)完整迁移方案(含历史连接记录、RDP 配置、保存的账号密码)【可行】 Endnote,如何能完整迁移库和模版 ubuntu 安装 locate “华为杯”第八届中国研究生人工智能创新大赛 当数据量很大时,执行 mysql> source filename.sql 很慢,有什么办法更快 pip install django-ranged-response==0.2.0 部署Vue+Django 04c,mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory Ubuntu Server 24.04 安装 MySQL, ./mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory ubuntu server 24.04: 如何设置默认采用 Xorg 方式登录 Ubuntu Server 24.04 设置锁屏时间 Ubuntu Server 启动过程中,比较慢 Job systemd-networkd-wait-online.service/start running Ubuntu Server 24.04 启用root用户 Ubuntu snap install 安装todesk是否必须检查 CPU 是否支持 AVX / AVX2 命令 Ubuntu Server 24.04下解决SunloginClient 向日葵依赖libgconf-2-4安装问题 Ubuntu Server 24.04 安装图形界面 ubuntu server 24.04 安装 中文输入法 Ubuntu Server 24.04 安装浏览器firefox Ubuntu Server 24.04 安装todesk【先联网,CPU必须支持 AVX / AVX2 】 Ubuntu Server 24.04 安装中文,汉字 Python赋值,什么时候传引用? 大驼峰命名法 django+mysql: 如何添加一个新的超级用户?
python 容易理解类和对象的例子
emanlee · 2026-04-07 · via 博客园 - emanlee

核心一句话

类 = 模板(比如 “手机”)

对象 = 用模板造出来的具体东西(比如 “你的 iPhone”“我的华为”)


最简单的例子:人类

1. 定义一个类(模板)

# 定义一个【人类】的模板(类)
class Person:
    # 初始化:给人设置名字、年龄
    def __init__(self, name, age):
        self.name = name  # 名字属性
        self.age = age    # 年龄属性

    # 人会做的动作(方法)
    def say_hello(self):
        print(f"大家好,我叫{self.name},我{self.age}岁啦!")

2. 用模板造对象(具体的人)

# 造第一个对象:小明
xiaoming = Person("小明", 18)
# 造第二个对象:小红
xiaohong = Person("小红", 20)

# 调用对象的功能
xiaoming.say_hello()
xiaohong.say_hello() 

运行结果

大家好,我叫小明,我18岁啦!
大家好,我叫小红,我20岁啦! 

更生活化的例子:手机类

这个更贴近生活,更好理解👇


# 模板:手机 class Phone: def __init__(self, brand, color): self.brand = brand # 品牌 self.color = color # 颜色 # 打电话功能 def call(self): print(f"{self.color}{self.brand}手机正在打电话!") # 造对象1:我的黑色苹果手机 my_phone = Phone("苹果", "黑色") # 造对象2:妈妈的白色华为手机 mom_phone = Phone("华为", "白色") # 使用对象 my_phone.call() mom_phone.call()
 

运行结果

黑色的苹果手机正在打电话!
白色的华为手机正在打电话! 

你只要记住这 3 点

  1. class 开头 = 做模板(类)
  2. __init__ = 给东西设置特征(属性)
  3. 造出来的具体东西 = 对象

类是图纸,对象是按图纸盖好的房子。

总结

  • :抽象模板(人、手机、汽车)
  • 对象:具体实例(小明、你的手机、爸爸的车)
  • 属性:长什么样(名字、颜色、品牌)
  • 方法:能做什么(说话、打电话、开车)