码迷,mamicode.com
首页 > 编程语言 > 详细

python学习笔记(八)面向对象扩展

时间:2017-10-10 20:34:26      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:turn   是什么   att   mis   nal   error:   注释   show   pre   

原链:http://www.cnblogs.com/vamei/archive/2012/06/02/2532018.html

笔记:今天的内容感觉自己理解不太容易

  1 #昨天学习定义move的时候,用到了self
  2 ‘‘‘
  3 def bird(object):
  4     have_feather = True
  5     way_of_reproduction = ‘egg‘
  6 
  7 summer = bird
  8 print (summer.way_of_reproduction)
  9 ‘‘‘
 10 #这里执行报错了
 11 #AttributeError: ‘function‘ object has no attribute ‘way_of_reproduction‘
 12 #上面用的define ,昨天用的class类,把上面都注释了,下面重新来
 13 
 14 class bird(object):
 15     have_feather = True
 16     way_of_reproduction = egg
 17 
 18 summer = bird
 19 print (summer.way_of_reproduction)
 20 
 21 #打印就是egg了,正常,下一步
 22 #然后是summer变为可以移动的鸡
 23 ‘‘‘
 24 class bird(object):
 25     have_feather = True
 26     way_of_reproduction = ‘egg‘
 27     def move(self, x, y):
 28         position = [0, 0]
 29         position[0] = position[0] + x
 30         position[1] = position[1] + y
 31         return position
 32 
 33 summer = bird
 34 print (summer(5,8))
 35 ‘‘‘
 36 #又报错了。。。TypeError: object() takes no parameters
 37 #忘了summer.move
 38 #这也说明了def move 也成为了bird的属性
 39 ‘‘‘
 40 class bird(object):
 41     have_feather = True
 42     way_of_reproduction = ‘egg‘
 43     def move(self, x, y):
 44         position = [0, 0]
 45         position[0] = position[0] + x
 46         position[1] = position[1] + y
 47         return position
 48 
 49 summer = bird
 50 print (summer.move(5, 8))
 51 #TypeError: move() missing 1 required positional argument: ‘y‘
 52 #。。。。不知道该说啥,昨天都正常打印啊
 53 ‘‘‘
 54 
 55 class bird(object):
 56     have_feather = True
 57     way_of_reproduction = egg
 58     def move(self, x, y):
 59         position = [0, 0]
 60         position[0] = position[0] + x
 61         position[1] = position[1] + y
 62         return position
 63 
 64 summer = bird()
 65 print (summer.move(5, 8))
 66 #bird后面少括号。。。。。
 67 #这里是self 的引入
 68 
 69 #作者原话:
 70 #self表示某个对象。对象拥有类的所有性质,那么我们可以通过self,调用类属性
 71 
 72 ‘‘‘
 73 class human(object):
 74     laugh = ‘hahahaha‘
 75     def show_laugh(self):
 76         print (self.laugh)
 77 
 78 #因为我们上面发现,def move,summer.move就可以调用move的属性
 79 #那这里,作者用了self.laugh,做个测试
 80 li_lei = human()
 81 print (li_lei.laugh)
 82 #输出结果:hahahaha
 83 #然后print (li_lei.show_laugh)
 84 #然后就。。。又懵逼了
 85 #输出:<bound method human.show_laugh of <__main__.human object at 0x000002A28D83EC50>>
 86 #不懂,继续看作者的吧
 87 ‘‘‘
 88 
 89 class human(object):
 90     laugh = hahahaha
 91     def show_laugh(self):
 92         print (self.laugh)
 93     def laugh_100th(self):
 94         for i in range(100):
 95             self.show_laugh()
 96 
 97 li_lei = human()
 98 li_lei.laugh_100th()
 99 
100 #这是作者的
101 #print (li_lei.laugh)  #输出是hahahaha
102 #直接li_lei.laugh      #输出是‘hahahaha‘,是字符串?
103 #这里后来明白了,summer.way_of_reproduction  输出是‘egg‘
104 
105 #print(li_lei.show_laugh())   #输出是hahahaha  None
106 #li_lei.show_laugh()          #输出是hahahaha
107 
108 #print(li_lei.laugh_100th())  #输出是100个 hahahaha  None
109 #li_lei.laugh_100th()         #输出是100个 hahahaha
110 
111 #这里不懂。。。。为啥?None 是什么?
112 
113 #laugh 是human 类 的属性,self是某个对象,拥有了类的所有属性
114 #self也就是拥有了human的属性
115 #定义了show_laugh功能,在这个功能里self用了laugh属性
116 #laugh_100th 用show_laugh的功能,show_laugh的功能就是self.laugh,也就是human 的laugh
117 
118 #不知道这么解释对不对
119 
120 
121 
122 
123 
124 #特殊方法  special method
125 class happybird(bird):#happybird 继承bird属性
126     def __init__(self, more_words):
127         print (We are happy birds., more_words)
128 
129 summer = happybird(happy, happy!)
130 #千万不要把init 达成int哈,报错。。。。
131 #自动调用,过程叫做初始化
132 #执行summer.__init__(more_words),把happy, happy  递给了more_words
133 #__init__()建立对象时自动执行

 技术分享

 

 

遗留:对象的性质

python学习笔记(八)面向对象扩展

标签:turn   是什么   att   mis   nal   error:   注释   show   pre   

原文地址:http://www.cnblogs.com/liyihao/p/7647292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!