码迷,mamicode.com
首页 > 其他好文 > 详细

CLASS

时间:2016-08-19 09:44:13      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

写在CLASS 前面:

  面向对象术语:类(class)——创建新类

         对象(object)——python中class的新格式,此object也是类,创建的新类即继承了这个类

         实例(instance)——实例和类就像泥鳅和鱼的关系,泥鳅有鱼的特性,即实例继承类的特性;泥鳅又有自己和其他鱼类不同的特点,即实例自身的独特性

         def——class中定义函数

         self——指代被访问的对象或者实例的一个变量?

         继承(inheritance)——一个类继承另一个类的特性

         组合(composition)——一个类将别的类作为部件(车和轮)

         是什么(is-a)

         有什么(has-a)

         

   class的一些表达方式:

     class X(Y)——make class named X that is-a Y

     class X(object):

       def __init__(self, j):

           ——class X has-a __init__ that takes self and j parameters

     class X (object):

       def M(self, j):

            ——class X has-a function named M that takes self and j parameters

              foo = X()   ——set foo to an instance of class X

     foo.M(J)    ——from foo get the function M and call it with self and j parameters

     foo.K = Q  ——from foo get K contribution and set it to Q

         

————————————————————————————————————————————————————————————————————————————————————

1. class 的 格式:

   class X(object):

      def __init__(self, y):

      def function_a(self, z):

class Animal(object):
    def __init__(self, name):
        self.name = name
        self.specialties = [ ]
        
    def voice(self, voice):
        self.voice = voice
        print voice

    def specialty(self, sp):
        self.specialties.append(sp)


        
d = Animal(dog)
d.voice(woooo~)
d.specialty(run)
print d.specialties

c = Animal(cat)
c.specialty(climb)
print c.specialties

 

2. 多重继承

CLASS

标签:

原文地址:http://www.cnblogs.com/nopear/p/5786213.html

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