面向对象编程
类:从一堆对象中以抽象的方式把相同的特征归类得到。
- 抽象类
- 类
- 实列
子类抽象为父类,子类继承父类特征。
类实例化为实例,实例抽象为类。
class Human(object):
century = 21
def __init__(self, name, age):
self.name = name
self.age = age
print("init work")
def speak(self, language):
print('%s has speak %s ability'% (self.name, language))
def write(self, word):
print('%s has write %s ability'% (self.name, word))
def walk(self):
print('%s has walk ability'% self)
Allen = Human('Allen-Cart', 16) # 输出结果:init work
print(Allen.name, Allen.age) # 输出结果:Allen-Cart 16
print(Allen.speak,Allen.write,Allen.walk)
# <bound method Human.speak of <__main__.Human object at 0x10ab50160>>
# <bound method Human.write of <__main__.Human object at 0x10ab50160>>
# <bound method Human.walk of <__main__.Human object at 0x10ab50160>>
print(Human.speak,Human.write,Human.walk)
# <function Human.speak at 0x110ba5378>
# <function Human.write at 0x110ba5400>
# <function Human.walk at 0x110ba5488>
Allen.speak("Chinese") # Allen-Cart has speak Chinese ability
Allen.write("Chinese") # Allen-Cart has write Chinese ability
Allen.walk() # <__main__.Human object at 0x10ab50160> has walk ability.
Human.walk('James') # James has walk ability
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现