23-python_クラスのメソッド
クラスのメソッド-共有メソッド-プライベートメソッド-クラスメソッド-静的メソッド
1公有の方法
-def methodName(self[,arg 1,...])を定義します.statements-instanceを呼び出します.methodName()
2プライベートメソッド
-def_を定義methodName(self [,arg1, ...]) : statements-クラス定義を呼び出すときに呼び出す.
3種類の方法
-クリーナー@classmethod-classmethod(function)->class method 3.1クリーナー-定義@classmethod def methodName(self):statements-classNameを呼び出す.methodName()3.2 classmethod(function)-定義def methodName(self):statements newMethodName=classmethod(methodName)-classNameを呼び出します.newMethodName()
4静的アプローチ
-装飾品@staticmethod-staticmethod(function)->static method 4.1装飾品-定義@staticmethod def methodName():statements-classNameを呼び出す.methodName()4.2 staticmethod(function)def methodName()statements newMethodName=staticmethod(methodName)-classNameを呼び出す.newMethodName()
5完全な例
1公有の方法
-def methodName(self[,arg 1,...])を定義します.statements-instanceを呼び出します.methodName()
2プライベートメソッド
-def_を定義methodName(self [,arg1, ...]) : statements-クラス定義を呼び出すときに呼び出す.
3種類の方法
-クリーナー@classmethod-classmethod(function)->class method 3.1クリーナー-定義@classmethod def methodName(self):statements-classNameを呼び出す.methodName()3.2 classmethod(function)-定義def methodName(self):statements newMethodName=classmethod(methodName)-classNameを呼び出します.newMethodName()
4静的アプローチ
-装飾品@staticmethod-staticmethod(function)->static method 4.1装飾品-定義@staticmethod def methodName():statements-classNameを呼び出す.methodName()4.2 staticmethod(function)def methodName()statements newMethodName=staticmethod(methodName)-classNameを呼び出す.newMethodName()
5完全な例
class Methods :
name = "hello"
def func1(self) :
print self.name, "func1 is public method"
def __func2(self) :
print self.name, "__func2 is private method"
#######################################################
def classFunc3(self) :
print self.name, "classFunc3 is class method"
@classmethod
def classFunc3_2(self) :
print self.name, "classFunc3_2 is class method"
classFunc3_3 = classmethod(classFunc3)
#######################################################
def staticFunc4() :
print "staticFunc4 is static method"
@staticmethod
def staticFunc4_2() :
print "staticFunc4_2 is static method"
staticFunc4_3 = staticmethod(staticFunc4)
if __name__ == "__main__" :
instance = Methods()
# call public method
instance.func1()
# call private method
try :
instance.__func2()
except AttributeError, msg :
print "call private failed!!!!!!!", msg
try :
Methods.classFunc3()
except TypeError, msg:
print msg
Methods.classFunc3_2()
Methods.classFunc3_3()
Methods.staticFunc4_2()
Methods.staticFunc4_3()