#類及物件
class
critter
(object):
"""a virtual pet"""
deftalk
(self)
:print
("hi,i'm instance of class critter."
)
crit = critter(
)crit.talk(
)
hi,i'm instance of class critter.
#構造器—自動呼叫
class
critter
(object):
"""a virtual pet"""
def__init__
(self)
:print
("a new critter has been born!"
)def
talk
(self)
:print
("\nhi,i'm an instance of class critter"
)crit1 = critter(
)crit2 = critter(
)crit1.talk(
)crit2.talk(
)
a new critter has been born!
a new critter has been born!
hi,i'm an instance of class critter
hi,i'm an instance of class critter
#建立物件和訪問物件的特性
class
critter
(object):
"""a virtual pet"""
def__init__
(self,name)
:print
("a new citter has been born!"
) self.name = name
def__str__
(self)
: rep =
"critter object\n"
rep +=
"name:"
+self.name+
"\n"
return rep
deftalk
(self)
:print
("hi,i'm"
,self.name,
"\n"
)
crit1 = critter(
"poochie"
)crit1.talk(
)crit2 = critter(
"randolph"
)crit2.talk(
)print
("printing crit1:"
)print
(crit1)
a new citter has been born!
hi,i'm poochie
a new citter has been born!
hi,i'm randolph
printing crit1:
critter object
name:poochie
#類特性和靜態方法
class
critter
(object):
"""a virtual pet"""
total =
0
@staticmethod
defstatus()
:print
("\nthe total number of critters is"
,critter.total)
def__init__
(self,name)
:print
("a critter has been born!"
) self.name = name
critter.total +=
1print
("accessing the class attribute critter.total:"
,end=
" ")
print
(critter.total)
print
("\ncreating critters."
)crit1 = critter(
"critter1"
)crit2 = critter(
"critter2"
)crit3 = critter(
"critter3"
)critter.status(
)print
("\naccessing the class attribute through an object:"
,end=
" ")
print
(crit1.total)
accessing the class attribute critter.total: 0
creating critters.
a critter has been born!
a critter has been born!
a critter has been born!
the total number of critters is 3
accessing the class attribute through an object: 3
#私有變數和方法,不給客戶端直接呼叫
class
critter
(object):
"""a virtual pet"""
def__init__
(self,name,mood)
:print
("a new critter has been born!"
) self.name = name
self.__mood = mood
deftalk
(self)
:print
("\ni'm"
,self.name)
print
("right now i feel"
,self.__mood,
"\n"
)def
__private_method
(self)
:print
("this is a private method."
)def
public_method
(self)
:print
("this is a public method."
) self.__private_method(
)
crit = critter(name =
"poochie"
,mood =
)crit.talk(
)crit._critter__private_method(
)crit.public_method(
)#crit.mood:報錯,只能通過類來訪問私有變數方法
a new critter has been born!
i'm poochie
this is a private method.
this is a public method.
this is a private method.
類的私有變數
1.在python 中可以通過在屬性變數名前加上雙下劃線定義屬性為私有屬性 特殊變數命名 案例 class a object zhe shi shuo ming name ling man def hello self print self.name print self.def get self ...
8 5 類的私有變數
1 2 3 4 5 6 7 8 9 10 11 12 13 classa object name zhou f defhello self print self.name print self.defget self returnself.a a print a.name a.hello print...
python類的私有變數和私有方法
usr bin env python coding utf 8 time 2017 11 08 8 46 author lijunjiang file class3.py 類的私有變數和私有方法 在python中可以通過在屬性變數名前加上雙下劃線定義屬性為私有屬性 特殊變數命名 1 xx 以單下劃線...