python 面向对象的类

参考《learn python hard way》

  网址:https://learnpythonthehardway.org/book/ex41.html

class X(Y)
"Make a class named X that is-a Y."
创建一个叫x的类,类x是y(类x 继承y),例如‘三文鱼‘是‘鱼’
class X(object): def __init__(self, J)
"class X has-a __init__ that takes self and J parameters."
类中有一个叫__init__,在__init__中有两个参数self 和J
  一般都会有__init__就是初始化,在没有类的函数中是从第一个非def的语句开始运行
  在如果有需要运行或是初始化的内容,可以加在__init__中
class X(object): def M(self, J)
"class X has-a function named M that takes self and J parameters."
类中定义了一个叫M的函数,M中有self和J的参数,self这个参数在类中的各个函数都必须有。
foo = X()
"Set foo to an instance of class X."
用foo来实例化类X
foo.M(J)
"From foo get the M function, and call it with parameters self, J."
从foo中获得函数M,然后调用函数M,函数M中的参数是J。
  这里的foo一定是实例化类后的
foo.K = Q
"From foo get the K attribute and set it to Q."
从foo中获得属性K,然后把它给K
  相当于self.chose = true或者M.color = 'blue'

posted on 2016-12-14 20:24  zhuzhu2016  阅读(208)  评论(0编辑  收藏  举报

导航