python面向对象开发 类的方法(精讲)

python

视频教程地址:http://v.youku.com/v_show/id_XNDg3NjQyNjQ4.html

 

代码规范!!!!

写出来的时候,

方法的地一个字母小写,从第二个字母开始,全部大写

类开头地一个字母大写!!!

class ExcellentPeople():

      def cleverChinese():

      def richGuys():

      def beautifulGirls():

按照这种方式!!!

千万不要写完代码后修改!!记住,第二个字母必须大写!!

类更大一点,第一个就需要大写!

-------------方法,第一个字母还是小写,后来的字母全部是大写!

视频上为什么到讲完了才说,然后自己改。怎么当程序员的,教学顺序有问题。

 

他们有

1 公有方法 public method

类中

2 私有方法 private method

只在类中被本类私有的,不能直接被外部被访问,或者只能通过类中其他方法访问,或者建立一个新的对象使用,object._Class__privatemethod()        # 这种方式一般在测试中使用,生产环境下不推荐

 

3 类方法 class method

使类中的方法,可以直接调出来供外部使用,节省内存,但是会花搜索类方法的时间,需要加self

4 静态方法 static method

将类中该方法所有的内容都加载到内存中,搜索快捷,但是不节约内存。不需要加self

公有方法:

私有方法:

 1 #!/usr/bin/python
 2 #coding:utf8
 3 
 4 class Milo():
 5     name = "csvt"
 6     __private = "这是一个私有属性"
 7     def fun1(self):
 8         print self.name
 9         print "我是公有方法"
10 
11     def __fun2(self):
12         print self.name
13         print "我是私有方法"
14 
15     @classmethod
16     def classfun(self):
17         print self.name
18         print "我是类方法"
19 
20     @staticmethod
21     def staticfun():            # 静态方法 不用 self! 因为已经全部加载了
22         print Milo.name         # 用类名选取类属性的表达方式!!! 不是self!
23 print "我是静态方法" 24 zou = Milo() 25 zou._Milo__fun2() # 通过这种方式调用私有方法,仅供测试用 26 # 一般情况下私有方法都是要在类中被别的方法利用的 27 print zou._Milo__private # 私有属性也是这样调用的,尽在测试中用

输出结果为:

alex@universe ~/python/OOP $ python cls_LeiDeFangFa_in_details.py 
csvt
我是私有方法
这是一个私有属性

 

调用私有方法的时候,还可以这样用:

 1 #!/usr/bin/python
 2 #coding:utf8
 3 
 4 class Milo():
 5     name = "csvt"
 6     __private = "这是一个私有属性"
 7     def fun1(self):
 8         print self.name
 9         print "我是公有方法"
10         self.__fun2()
11 
12     def __fun2(self):
13         print self.name
14         print "我是私有方法"
15 
16     @classmethod
17     def classfun(self):
18         print self.name
19         print "我是类方法"
20 
21     @staticmethod
22     def staticfun():           # 静态方法不用加 self
23         print Milo.name # 用类名选取类属性的表达方式!!! 不是self!
24         print "我是静态方法"
25 zou = Milo()
26 zou.fun1()              # 通过这种方式调用私有方法
27                                 # 一般情况下私有方法都是要在类中被别的方法利用的
                                                                                 

输出结果为:

alex@universe ~/python/OOP $ python cls_LeiDeFangFa_in_details.py 
csvt
我是公有方法
csvt
我是私有方法

-----提示:

公有属性可以直接这样访问

print Milo.name

输出是

csvt

 

但是公有方法不能这样访问,必须实例化(创造一个实例)

Milo.fun1()

报错如下:

alex@universe ~/python/OOP $ python cls_LeiDeFangFa_in_details.py 
Traceback (most recent call last):
  File "cls_LeiDeFangFa_in_details.py", line 26, in <module>
    Milo.fun1()
TypeError: unbound method fun1() must be called with Milo instance as first argument (got nothing instead)

 

类方法:

比较简单的是使用装饰器,即 @classmethod

 1 #!/usr/bin/python
 2 #coding:utf8
 3 
 4 class Milo():
 5     name = "csvt"
 6     def fun1(self):
 7         print self.name
 8         print "我是公有方法"
 9         self.__fun2()
10 
11     def __fun2(self):
12         print self.name
13         print "我是私有方法"
14 
15     @classmethod
16     def classfun(self):
17         print self.name
18         print "我是类方法"
19 
20     @staticmethod
21     def staticfun(): # 静态方法 不用 self! 因为已经全部加载了
22         print Milo.name  # 用类名选取类属性的表达方式!!! 不是self!
23         print "我是静态方法"
24 
25 Milo.classfun()

输出的是

alex@universe ~/python/OOP $ python cls_LeiDeFangFa_in_details.py 
csvt
我是类方法

 

如果不加类方法的话(即去掉@classmethod,或者不使用newClassFun = classmethod(classfun) 的这种新建一个类属性的方法),则不能直接用

 

报错:

alex@universe ~/python/OOP $ python cls_LeiDeFangFa_in_details.py 
Traceback (most recent call last):
  File "cls_LeiDeFangFa_in_details.py", line 25, in <module>
    Milo.classfun()
TypeError: unbound method classfun() must be called with Milo instance as first argument (got nothing instead)

 

 

静态方法:

用@staticmethod

用 staticnewfun = staticmethod(staticfun)   # 新建一个静态方法,看起来比较麻烦

都可以达到将方法变成静态方法 的 目的。

 1 #!/usr/bin/python
 2 #coding:utf8
 3 
 4 class Milo():
 5     name = "csvt"
 6     def fun1(self):
 7         print self.name
 8         print "我是公有方法"
 9         self.__fun2()
10 
11     def __fun2(self):
12         print self.name
13         print "我是私有方法"
14 
15     @classmethod
16     def classfun(self):
17         print self.name
18         print "我是类方法"
19 
20     @staticmethod
21     def staticfun():
22         print Milo.name
23         print "我是静态方法"
24 
25 Milo.staticfun()

注意上面的

20     @staticmethod
21     def staticfun():
22         print Milo.name
23         print "我是静态方法"

statcitfun()没有self,表示全部加载了。

Milo.name 直接用的类名找属性了

输出结果是:

alex@universe ~/python/OOP $ python cls_LeiDeFangFa_in_details.py 
csvt
我是静态方法

 

你可能感兴趣的:(python)