exercise 40 模块 类 对象

来源:互联网 发布:淘宝首页素材 编辑:程序博客网 时间:2024/05/22 17:00
一、模块:
  1. 一个python文件,里面有很多变量和函数
  2. 你可以导入这个文件
  3. 你可以用 . 来访问模块中的函数和变量

范例:
想象一下,我有一个包含apple函数和变量tangerine的模块文件mystuff.py:
def apple():
    print "I am apples!"
tangerine = "Living reflection of a dream"
我可以用import导入mystuff模板使用apple函数
import mystuff
mystuff.apple()
print mystuff.tangerine

模块和字典的对比:

字典:mystuff['apple']       # get apple from dict
模块:mystuff.apple()      # get apple from the module
mystuff.tangerine# same thing, it's just a variable

这表明,python有一种共同的模式:
  1. 建立一种key=value的模式
  2. 使用key的名称调用value
在字典中key是字符串(我觉得应该是关键字的意思吧),调用句法是dict['key'],在模块中,key是标示符,调用方法是 key.py


二、类:
模块是特殊的字典,它可以保存python代码。通过 . 号调用。
python还有一个类似实现这种目的的结构,叫做类。一个类包含了很多函数和数据,可以通过 . 去访问它们。

同上mystuff功能的类:
class mystuff(object):
    def __int__(self):
        self.tangerine = "Hello"

    def apple(self):
        print "apple"

和模块比有点复杂,不过你可以认为它就是一个迷你模块。让人疑惑的是__init__()函数和self.tangerine设置tangerine变量的值。
用类替代模块的原因是:一个程序中只能导入一个模块,但可以使用同一个类多次。


三、对象就像导入(objects are like import)
如果类像模块,那么类也会有类型模块的导入功能。就是实例化,如果你实例化一个类,得到的是一个对象。
使用类的方法类似调用函数
thing = mystuff()
thing.apple()
print thing.tangerine

第一步是实例化,然后调用它的函数,我们通过上面的代码分析一下python是怎么按照顺序执行的:
  1. python寻找Mystuff,看看你是不是定义了这个类。(Python looks for MyStuff() and sees that it is a class you've defined.)
  2. python为你在类里面定义的函数创建一个空对象。(Python crafts an empty object with all the functions you've specified in the class usingdef.)
  3. 如果类中有__init__函数,那么就会使用这个函数初始化你的空对象。(Python then looks to see if you made a "magic"__init__ function, and if you have it calls that function to initialize your newly created empty object.)
  4. 在__init__方法中有一个额外的变量self,这就是python为我们创建的空对象,你可以给这个变量赋值。(In theMyStuff function__init__ I then get this extra variableself, which is that empty object Python made for me, and I can set variables on it just like you would with a module, dictionary, or other object.)
  5. 这样的话,我给 thing.tangerine赋了句歌词,并且初始化了这个对象。(In this case, I setself.tangerine to a song lyric and then I've initialized this object.)
  6. 那么现在python就可以把这个刚完成的对象赋给一个变量thing了。(Now Python can take this newly minted object and assign it to thething variable for me to work with.)

至此仍旧没明白类和对象的含义!!!!!


范例:
class Song(object):    def __init__(self, lyrics):  #__init__这个函数称为构造函数,对类进行初始化.        self.lyrics = lyrics    def sing_me_a_song(self):        for line in self.lyrics:            print linehappy_bday = Song(["Happy birthday to you",                   "I don't want to get sued",                   "So I'll stop right there"])bulls_on_parade = Song(["They rally around tha family",                        "With pockets full of shells"])happy_bday.sing_me_a_song()bulls_on_parade.sing_me_a_song()


What you see:
Happy birthday to you
I don't want to get sued
So I'll stop right there
They rally around tha family
With pockets full of shells



构造函数:__init__对类进行初始化
实例化:当类实例化后立刻运行__init__()函数

self干嘛的?????


小总结:
1.class 中有私有属性attribute  无法在外部使用
2.私有属性或函数以两个下划线开头__不一定结尾???。






——————————————先理解到这里


append是添加一个item,而extend需要参数是一个iterator
extend()接受一个列表参数,把参数列表的元素添加到列表的尾部,append()接受一个对象参数,把对象添加到列表的尾部
[1,2].extend([1,2,3])
[1,2,1,2,3]
[1,2].append([1,2,3])
[1,2,[1,2,3]]


什么是面向对象编程的思想:
解释1:
给你一个任务,你要思考怎么做。
如果你的思维方式是:我先做什么,再做什么……这叫面向过程;
如果思维方式是:我先做一个什么东西来做这件事,再做一个什么东西来做那件事,然后它们怎么相互配合……这叫面向对象

解释2:
面向过程的通常是动宾形式结构(操作符,运算对象。函数,参数),从自然语法角度看就是谓语加宾语。
面向对象的通常是主谓宾结构(对象,方法,参数)。
面向对象更符合自然语言的思维习惯,程序结构的组织就是独立的个体,通过消息(参数)传递进行交互。

解释3:
封装,个体独立,不需要具体了解其工作实现,熟悉接口即可。
继承,个体功能扩展。
多态,个体在不同场景下的处理方式。









0 0