python object-oriented _1

self 

# Self corresponds to this pointer in c ++ 
class Ball:
     DEF the setName (Self, name):
        self.name = name
    def kick(self):
        print('我叫%s'%self.name)

ball = Ball()
ball.setName ( ' football ' )
ball.kick()
ball1 = Ball()
ball1.setName ( ' basketball ' )
ball1.kick()

class ball1:
     DEF  the __init__ (Self, name, ID = ' XXX ' ): # constructor initializes the object instance is automatically invoked when 
        the self.name = name
        self.id = the above mentioned id
     DEF Kick (Self):
         Print ( ' My name is% s,% s I is the world's first big ball ' % (self.name, self.id))
B = ball1 ( ' volleyball ' , ' three ' )
b.kick()

 

Guess you like

Origin www.cnblogs.com/mhmh007/p/11837142.html
Recommended