Python 面向对象

一、定义

函数:

    def + 函数名(参数名)

面向对象:

   class  + 类名

 

二、执行

函数:  函数名(参数)

面向对象: o=类名()   =》实例,对象

                   o.foo()

定义并执行类中的方法:

class 类名:

   def  方法名(self,args):

         print (args)

         return 1

对象=类名()

ret = 对象.方法名(args)

print (ret)

self 指的是调用方法的对象

 1 class Bar():
 2     def foo(self,args):
 3         print(self,self.name,self.age,self.gender,args)
 4 b1=Bar()
 5 b1.name='alex'
 6 b1.age=18
 7 b1.gender='male'
 8 print(b1)
 9 b1.foo(66)
10 print('---------------')
11 b2=Bar()
12 b2.name='gyc'
13 b2.age=24
14 b2.gender='female'
15 print(b2)
16 b2.foo(6666)
17 #<__main__.Bar object at 0x000001F1D1C42198>
18 #<__main__.Bar object at 0x000001F1D1C42198> alex 18 male 66
19 #---------------
20 #<__main__.Bar object at 0x000001F1D1C42240>
21 #<__main__.Bar object at 0x000001F1D1C42240> gyc 24 female 6666
View Code

 

posted on 2018-01-16 23:14  可爱的春哥  阅读(45)  评论(0)    收藏  举报

导航