Python 中的面向对象编程入门
1. 面向对象编程概述
面向对象编程(OOP)是程序员工具箱中的强大工具。在 Python 中,一切皆为对象,像列表、字符串、整数、函数等数据类型和数据结构都是对象。例如:
>>> type([1, 2, 3])
<class 'list'>
>>> type("foobar")
<class 'str'>
>>> type({"a": 1, "b": 2})
<class 'dict'>
>>> def func(): return True
...
>>> type(func)
<class 'function'>
每个类就像是对象的蓝图,定义了对象应具备的属性和方法。比如列表类的所有实例都有 append() 方法,可用于向列表添加元素:
>>> l = [1, 2, 3, 4, 5] # create a list object
>>> print(l.append)
<built-in method append of list object at 0x10dd36a08>
>>> print(l.remove)
<built-in method remove of list object at 0x10dd36a08>
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



