编程小白的第一本 python 入门书 学习笔记06 第七章 类与可口可乐
2018-02-10 16:01
1051 查看
定义一个可口可乐类,类中有一个列表变量。类的变量又称为类的属性。class CocaCola:formula = ['caffeine','sugar','water','soda'] 类的实例化和属性的使用coke_for_me= CocaCola()coke_for_you= CocaCola() print(CocaCola.formula)print(coke_for_me.formula)print(coke_for_you.formula) >>> ['caffeine','sugar','water','soda']>>> ['caffeine','sugar','water','soda']>>> ['caffeine','sugar','water','soda'] for element in coke_for_me.formula:print(element) >>> caffeine>>> sugar>>> water>>> soda 创建实例属性(对象属性),及通过实例对象创建和使用类中不存在的属性。class CocaCola:formula =['caffeine','sugar','water','soda']' coke_for_China= CocaCola()coke_for_China.local_logo = '可口可乐 'print(coke_for_China.local_logo) 使用实例方法class CocaCola:formula = ['caffeine','sugar','water','soda']def drink(self):print('Energy!') coke= CocaCola()coke.drink() >>> Energy! self的解释coke= CocaColacoke.drink() == CocaCola.drink(coke) 被实例化的对象会被编译器默默地传入后面方法的括号中,作为第一个参数。self这个参数名称是可以随意修改名称的,但是按照Python的规定,我们统一使用self。 我们还可以加入自己的参数class CocaCola:formula = ['caffeine','sugar','water','soda']def drink(self,how_much):if how_much== 'asip':print('Cool~')elif how_much== 'wholebottle'print('Headache!') ice_coke= CocaCola()ice_coke.drink('asip') >>> Cool~ 魔术方法(如初始化方法 _init_(),创建实例的时候,自动执行的方法 )class CocaCola:formula = ['caffeine','sugar','water','soda']def __init__(self,logo_name):self.local_logo = logo_namedef drink(self):print('Energy!') coke = CocaCola('可口可乐')coke.local_logo>>> 可口可乐 类的继承父类class CocaCola:calories = 140sodium = 45total_carb = 39caffeine = 34 ingredients = ['High Fructose Corn Syrup','Carbonated Water','Phosphoric Acid','Natural Flavors','Caramel Color','Caffeine'] def __init__(self,logo_name):self.local_logo = logo_name def drink(self):print('You got {} cal energy!'.format(self.calories)) 子类class CaffeineFree(CocaCola):caffeine = 0 ingredients = ['High Fructose Corn Syrup','Carbonated Water','Phosphoric Acid','Natural Flavors','Caramel Color',] coke_a= CaffeineFree('Cocacola-FREE')coke_a.drink() 一些需要注意的地方类属性如果被重新赋值,是否会影响到类属性的引用?
只有实例属性是实例自己的,其他属性来自类。classTestA:attr=1 obj_a=TestA()obj_a.test='test' TestA.attr=42print(obj_a.attr) print(TestA.__dict__)print(obj_a.__dict__) >>>42>>>{'__module__':'第八章.开始使用第三方类库','attr': 42, '__dict__': <attribute '__dict__' of 'TestA' objects>,'__weakref__': <attribute '__weakref__' of 'TestA' objects>, '__doc__':None}>>>{'test': 'test'} 实例属性如果被重新赋值,是否会影响到类属性的引用?
如果改变实例属性的值,那么实例属性的值就来自自身。classTestA:attr=1 obj_a=TestA()obj_b=TestA() obj_a.attr=42print(obj_b.attr)print(TestA.__dict__)print(obj_a.__dict__) >>>1>>>{'__module__':'第八章.开始使用第三方类库','attr': 1, '__dict__': <attribute '__dict__' of 'TestA' objects>,'__weakref__': <attribute '__weakref__' of 'TestA' objects>, '__doc__':None}>>>{'attr': 42} 类属性实例属性具有相同的名称,那么.后面引用的将会是什么?
按照代码执行顺序,最后引用的会是实例属性。classTestA:attr=1def__init__(self):self.attr=42 obj_a=TestA()print(TestA.attr)print(obj_a.attr) >>>1>>>42 类的扩展obj1=1obj2='String!'obj3=[]obj4={}print(type(obj1),type(obj2),type(obj3),type(obj4)) >>><class 'int'> <class 'str'> <class 'list'><class 'dict'> 安装自己的库一般需要提交到pip上,简易方法是把python文件放到 site-packages文件夹。import sysprint(sys.path) print(sys.path[3])
只有实例属性是实例自己的,其他属性来自类。classTestA:attr=1 obj_a=TestA()obj_a.test='test' TestA.attr=42print(obj_a.attr) print(TestA.__dict__)print(obj_a.__dict__) >>>42>>>{'__module__':'第八章.开始使用第三方类库','attr': 42, '__dict__': <attribute '__dict__' of 'TestA' objects>,'__weakref__': <attribute '__weakref__' of 'TestA' objects>, '__doc__':None}>>>{'test': 'test'} 实例属性如果被重新赋值,是否会影响到类属性的引用?
如果改变实例属性的值,那么实例属性的值就来自自身。classTestA:attr=1 obj_a=TestA()obj_b=TestA() obj_a.attr=42print(obj_b.attr)print(TestA.__dict__)print(obj_a.__dict__) >>>1>>>{'__module__':'第八章.开始使用第三方类库','attr': 1, '__dict__': <attribute '__dict__' of 'TestA' objects>,'__weakref__': <attribute '__weakref__' of 'TestA' objects>, '__doc__':None}>>>{'attr': 42} 类属性实例属性具有相同的名称,那么.后面引用的将会是什么?
按照代码执行顺序,最后引用的会是实例属性。classTestA:attr=1def__init__(self):self.attr=42 obj_a=TestA()print(TestA.attr)print(obj_a.attr) >>>1>>>42 类的扩展obj1=1obj2='String!'obj3=[]obj4={}print(type(obj1),type(obj2),type(obj3),type(obj4)) >>><class 'int'> <class 'str'> <class 'list'><class 'dict'> 安装自己的库一般需要提交到pip上,简易方法是把python文件放到 site-packages文件夹。import sysprint(sys.path) print(sys.path[3])
相关文章推荐
- 编程小白的第一本 python 入门书 学习笔记07 第八章 开始使用第三方类库
- 编程小白的第一本 python 入门书 学习笔记04 第五章 循环与判断
- 编程小白的第一本 python 入门书 学习笔记02 第三章 变量与字符串
- 编程小白的第一本 python 入门书 学习笔记01 第二章 安装Python环境
- Python(编程小白的第一本 Python 入门书) 学习笔记2——代码练习
- 编程小白的第一本 python 入门书 学习笔记05 第六章 数据结构
- 笔记|《简明Python教程》:编程小白的第一本python入门书
- 笔记|《简明Python教程》:编程小白的第一本python入门书
- 编程小白的第一本 python 入门书pdf
- 编程小白的第一本python入门书
- 《编程小白的第一本 Python 入门书》 PDF
- python小白入门学习笔记-爬虫入门
- Python 入门(一)——《编程小白的第一本 Python 入门书》学习笔记
- <Python编程从入门到实践--web应用程序> 学习笔记
- [Python入门及进阶笔记00]-写在前面(目录/书籍/学习路线/其他)
- 用Python和Pygame写游戏-从入门到精通(1)学习笔记
- SQL Server 2008编程入门经典笔记(第七章:更复杂的查询)
- Go学习笔记 - 关于Java、Python、Go编程思想的不同
- 游戏编程入门学习笔记26——AI篇——AI的实现
- Python学习笔记--多线程编程,thread