mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Sec. 6 translated. All sections translated. Hooray.
This commit is contained in:
parent
3544a43d11
commit
2ecf370ce4
@ -567,60 +567,59 @@ class Human(object):
|
|||||||
def say(self, msg):
|
def say(self, msg):
|
||||||
return "{0}: {1}".format(self.name, msg)
|
return "{0}: {1}".format(self.name, msg)
|
||||||
|
|
||||||
# A class method is shared among all instances
|
# 一個類別方法會被所有的實體所共用
|
||||||
# They are called with the calling class as the first argument
|
# 他們會以類別為第一參數的方式被呼叫
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_species(cls):
|
def get_species(cls):
|
||||||
return cls.species
|
return cls.species
|
||||||
|
|
||||||
# A static method is called without a class or instance reference
|
# 靜態方法
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def grunt():
|
def grunt():
|
||||||
return "*grunt*"
|
return "*grunt*"
|
||||||
|
|
||||||
# A property is just like a getter.
|
# 屬性就像是用getter取值一樣
|
||||||
# It turns the method age() into an read-only attribute
|
# 它將方法 age() 轉為同名的、只能讀取的屬性
|
||||||
# of the same name.
|
|
||||||
@property
|
@property
|
||||||
def age(self):
|
def age(self):
|
||||||
return self._age
|
return self._age
|
||||||
|
|
||||||
# This allows the property to be set
|
# 這樣寫的話可以讓屬性被寫入新的值
|
||||||
@age.setter
|
@age.setter
|
||||||
def age(self, age):
|
def age(self, age):
|
||||||
self._age = age
|
self._age = age
|
||||||
|
|
||||||
# This allows the property to be deleted
|
# 這樣寫的話允許屬性被刪除
|
||||||
@age.deleter
|
@age.deleter
|
||||||
def age(self):
|
def age(self):
|
||||||
del self._age
|
del self._age
|
||||||
|
|
||||||
|
|
||||||
# Instantiate a class
|
# 將類別實體化
|
||||||
i = Human(name="Ian")
|
i = Human(name="Ian")
|
||||||
print i.say("hi") # prints out "Ian: hi"
|
print i.say("hi") # prints out "Ian: hi"
|
||||||
|
|
||||||
j = Human("Joel")
|
j = Human("Joel")
|
||||||
print j.say("hello") # prints out "Joel: hello"
|
print j.say("hello") # prints out "Joel: hello"
|
||||||
|
|
||||||
# Call our class method
|
# 呼叫類別方法
|
||||||
i.get_species() # => "H. sapiens"
|
i.get_species() # => "H. sapiens"
|
||||||
|
|
||||||
# Change the shared attribute
|
# 更改共用的屬性
|
||||||
Human.species = "H. neanderthalensis"
|
Human.species = "H. neanderthalensis"
|
||||||
i.get_species() # => "H. neanderthalensis"
|
i.get_species() # => "H. neanderthalensis"
|
||||||
j.get_species() # => "H. neanderthalensis"
|
j.get_species() # => "H. neanderthalensis"
|
||||||
|
|
||||||
# Call the static method
|
# 呼叫靜態方法
|
||||||
Human.grunt() # => "*grunt*"
|
Human.grunt() # => "*grunt*"
|
||||||
|
|
||||||
# Update the property
|
# 更新屬性
|
||||||
i.age = 42
|
i.age = 42
|
||||||
|
|
||||||
# Get the property
|
# 取得屬性
|
||||||
i.age # => 42
|
i.age # => 42
|
||||||
|
|
||||||
# Delete the property
|
# 移除屬性
|
||||||
del i.age
|
del i.age
|
||||||
i.age # => raises an AttributeError
|
i.age # => raises an AttributeError
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user