Instance Attribute
)Class Attribute
)Instance Attribute
)¶## 範例
class Animal():
def __init__(self, name, obj_type):
#instance attribute
self.name = name
self.obj_type = obj_type
John = Animal('John', 'man')
print(John.name)
print(John.obj_type)
Mary = Animal('Mary', 'woman')
print(Mary.name)
print(Mary.obj_type)
print('- - - - - - - -')
John.obj_type = 'old man' # 修改物件John的屬性, 並不影響Mary的屬性
print(John.name)
print(John.obj_type)
print(Mary.name)
print(Mary.obj_type)
John man Mary woman - - - - - - - - John old man Mary woman
Class Attribute
)¶## 範例
class Animal():
#class attribute
classification = 'human'
def __init__(self, name, obj_type):
#instance attribute
self.name = name
self.obj_type = obj_type
John = Animal('John', 'man')
print(John.name)
print(John.obj_type)
print(John.classification) ## 透過物件呼叫類別屬性
print('- - - - -')
Mary = Animal('Mary', 'woman')
print(Mary.name)
print(Mary.obj_type)
print(Mary.classification) ## 透過物件呼叫類別屬性
print('- - - - - - - - - - - - - - - -')
print(Animal.classification) ## 注意寫法 - 透過類別呼叫類別屬性
Animal.classification = 'person' ## 注意寫法 - 修改類別屬性
print(Animal.classification, id(Animal.classification))
print(John.classification, id(John.classification))
print(Mary.classification, id(Mary.classification))
print('- - - - - - - - - - - - - - - -')
John.classification = 'boy' ##注意寫法 - 透過物件更改類別屬性, 等同於產生新的實體屬性
print(Animal.classification, id(Animal.classification))
print(John.classification, id(John.classification))
print(Mary.classification, id(Mary.classification))
print('- - - - - - - - - - - - - - - -')
print(Animal.name) ## 類別無法呼叫實體屬性
John man human - - - - - Mary woman human - - - - - - - - - - - - - - - - human person 4443776304 person 4443776304 person 4443776304 - - - - - - - - - - - - - - - - person 4443776304 boy 4443811120 person 4443776304 - - - - - - - - - - - - - - - -
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-3-a1019e29f5e9> in <module> 31 print(Mary.classification, id(Mary.classification)) 32 print('- - - - - - - - - - - - - - - -') ---> 33 print(Animal.name) ## 類別無法呼叫實體屬性 AttributeError: type object 'Animal' has no attribute 'name'
## 範例 - 當物件屬性與類別屬性相同的狀況(無法使用物件呼叫類別屬性)
class Animal():
#class attribute
type_attribute = 'human'
def __init__(self, name, type_attribute):
#instance attribute
self.name = name
self.type_attribute = type_attribute
John = Animal('John', 'man')
print(John.name)
print(John.type_attribute)
print('- - - - -')
Mary = Animal('Mary', 'woman')
print(Mary.name)
print(Mary.type_attribute)
print('- - - - - - - - - - - - - - - -')
print(Animal.type_attribute)
Animal.type_attribute = 'person'
print(Animal.type_attribute, id(Animal.type_attribute))
print(John.type_attribute, id(John.type_attribute))
print(Mary.type_attribute, id(Mary.type_attribute))
John man - - - - - Mary woman - - - - - - - - - - - - - - - - human person 4576950640 man 4550032688 woman 4576888240
## 範例 - 透過物件修改類別屬性
class Animal():
#class attribute
classification = 'human'
def __init__(self, name, obj_type):
#instance attribute
self.name = name
self.obj_type = obj_type
def reset_classVariable_classification(self):
self.__class__.classification = 'monkey'
John = Animal('John', 'boy')
print(John.classification)
John.reset_classVariable_classification() #透過物件實體修改類別屬性
print(John.classification, id(John.classification))
print(Animal.classification, id(Animal.classification))
human monkey 4550202992 monkey 4550202992
## 猜猜以下執行結果
class Animal():
father_attribute = 'animal'
def __init__(self, name):
self.name = name
class Human(Animal):
son_attribute = 'Human'
def __init__(self, name):
self.name = name
print(Animal.father_attribute)
print(Human.son_attribute)
print('- '*5)
John = Human('John')
print(John.son_attribute)
print(John.father_attribute)
print('- '*5)
Bird = Animal('Bird')
print(Bird.father_attribute)
print(Bird.son_attribute)
## 猜猜以下執行結果
class Animal():
__father_attribute = 'animal'
def __init__(self, name):
self.name = name
def get_attribute(self):
return self.__class__.__father_attribute
print(Animal.__father_attribute)
Bird = Animal('Bird')
print(Bird.get_attribute())
Class Method
)¶Instance Method
)Class Method
)Static Method
)Instance Method
)¶self
參數## 範例
class Animal():
#class attribute
classification = 'human'
def __init__(self, name, obj_type):
#instance attribute
self.name = name
self.obj_type = obj_type
#instance method
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
John = Animal('John', 'man')
print(John.get_name())
John.set_name('Jimmy')
print(John.get_name())
John Jimmy
Class Method
)¶@classmethod
, 並帶入屬性cls
@classmethod
撰寫於方法之前, 表示所撰寫的為類別方法cls
## 範例
class Animal():
#class attribute
classification = 'human'
def __init__(self, name, obj_type):
#instance attribute
self.name = name
self.obj_type = obj_type
#instance method
def get_name(self):
print(self)
return self.name
def set_name(self, name):
self.name = name
#class method
@classmethod
def eatting(cls):
print(cls)
print('%s 具備eatting技能' %(cls.classification))
John = Animal('John', 'man')
print(John.get_name())
print('- - - - - - - ')
John.eatting() #物件呼叫類別方法
print('- - - - - - - ')
Animal.eatting() #類別呼叫類別方法
<__main__.Animal object at 0x108ec0a10> John - - - - - - - <class '__main__.Animal'> human 具備eatting技能 - - - - - - - <class '__main__.Animal'> human 具備eatting技能
## 範例 - 透過類別方法產生物件
class Animal():
#class attribute
classification = 'human'
def __init__(self, name, obj_type):
#instance attribute
self.name = name
self.obj_type = obj_type
#instance method
def get_name(self):
print(self)
return self.name
def set_name(self, name):
self.name = name
#class method
@classmethod
def Woman(cls, name):
return cls(name, 'woman') #建構Animal物件
@classmethod
def Man(cls, name):
return cls(name, 'man') #建構Animal物件
Mary = Animal.Woman('Mary')
print(Mary.name)
print(Mary.obj_type)
print(Mary.get_name())
print('- - - - - - - ')
John = Animal.Man('John')
print(John.name)
print(John.obj_type)
print(John.get_name())
Mary woman <__main__.Animal object at 0x10731b310> Mary - - - - - - - John man <__main__.Animal object at 0x108ec0710> John
## 猜猜以下執行結果
class Animal():
father_attribute = 'animal'
def __init__(self, name):
self.name = name
def get_info(self):
return Animal.get_attribute_info()
@classmethod
def get_attribute_info(cls):
return cls.father_attribute
Bird = Animal('Bird')
print(Bird.get_info())
## 猜猜以下執行結果
class Animal():
father_attribute = 'animal'
def __init__(self, name):
self.name = name
@classmethod
def get_animal_info(cls):
return cls.father_attribute
class Human(Animal):
son_attribute = 'Human'
def __init__(self, name):
self.name = name
@classmethod
def get_human_info(cls):
return cls.son_attribute
print('- '*5)
John = Human('John')
print(John.get_animal_info())
print(John.get_human_info())
## 猜猜以下執行結果
class Animal():
father_attribute = 'animal'
def __init__(self, name):
self.name = name
@classmethod
def get_attribute_info(cls):
return cls.father_attribute
class Human(Animal):
son_attribute = 'Human'
__son_gender = 'man'
def __init__(self, name):
self.name = name
@classmethod
def get_attribute_info(cls):
return cls.son_attribute
@classmethod
def get_gender(cls):
return cls.__son_gender
John = Human('John')
print(John.get_attribute_info())
print(John.get_gender())
## 猜猜以下執行結果
class Animal():
__private_father_attribute = 'animal'
def __init__(self, name):
self.name = name
@classmethod
def __class_father_private_method(cls):
return 'this is class private method'
@classmethod
def class_father_public_method(cls):
return cls.__class_father_private_method();
class Human(Animal):
__private_son_attribute = 'human'
def __init__(self, name):
self.name = name
@classmethod
def get_father_private_method(cls):
return Animal.__class_father_private_method()
def son_instance_methon_get_father_private_method(self):
return self.__class__.__class_father_private_method()
@classmethod
def class_son_public_method(cls):
return cls.__class_father_private_method();
John = Human('John')
print(John.get_father_private_method())
print(John.__class_father_private_method())
print(John.class_father_public_method())
print(John.son_instance_methon_get_father_private_method())
print(John.class_son_public_method())
print(Human.__class_father_private_method())
print(Human.class_father_public_method())
@staticmethod
, 靜態方法不需先寫任何參數(self or cls
)@staticmethod
撰寫於方法之前, 表示所撰寫的為靜態方法## 範例
class Animal():
#class attribute
classification = 'human'
def __init__(self, name, obj_type):
#instance attribute
self.name = name
self.obj_type = obj_type
#instance method
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
#staticmethod
@staticmethod
def Playing_Time():
return '今天玩了一個下午'
John = Animal('John', 'boy')
print(John.get_name())
print(John.Playing_Time()) #物件呼叫靜態方法
print(Animal.Playing_Time()) #類別呼叫靜態方法
John 今天玩了一個下午 今天玩了一個下午
## 猜猜以下執行結果
class Animal():
classification = 'animal'
def __init__(self, obj_type):
self.obj_type = obj_type
@staticmethod
def Animal_static():
return Animal.classification
animal = Animal('animal')
animal.Animal_static()
## 猜猜以下執行結果
class Animal():
classification = 'animal'
def __init__(self, obj_type):
self.obj_type = obj_type
@staticmethod
def Animal_static():
return Animal.classification
class Humal(Animal):
def __init__(self, obj_type):
self.obj_type = obj_type
@staticmethod
def get_father_static_method():
return Animal_static()
John = Humal('Human')
print(John.Animal_static())
print(John.get_father_static_method())
@property #將方法轉換為「只能讀取」的屬性(透過此修飾器達到封裝效果)
## 範例
class Bank():
def __init__(self, name):
self.name = name
@property
def password(self):
return 'my_password'
Richard = Bank('Richard')
print(Richard.password) #注意因為property將方法轉為屬性, 故不需要括號()
Richard.password = 'Change Passwd' #只可讀取不可修改
my_password
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-5-3b24134b3e96> in <module> 10 Richard = Bank('Richard') 11 print(Richard.password) #注意因為property將方法轉為屬性, 故不需要括號() ---> 12 Richard.password = 'Change Passwd' #只可讀取不可修改 AttributeError: can't set attribute
@property
## 猜猜以下執行結果
class Bank():
def __init__(self, name):
self.name = name
@property
@staticmethod
def password():
return 'my_password'
Richard = Bank('Richard')
print(Richard.password)
@property
轉屬性後搭配使用¶_________.setter #___________為轉換的屬性名稱
_________.deleter #___________為轉換的屬性名稱
## 範例
class Bank():
def __init__(self, name):
self.name = name
self.word = 'My password'
@property
def password(self):
return self.word
@password.setter #注意寫法
def password(self, value):
self.word = value
@password.deleter #注意寫法
def password(self):
del self.word
print('delete self.password')
Richard = Bank('Richard')
print(Richard.password) #注意因為property將方法轉為屬性, 故不需要括號()
Richard.password = "change password" #透過setter可重新定義password
print(Richard.password)
del Richard.password
print(Richard.password)
My password change password delete self.password
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-6-a8466536abca> in <module> 23 print(Richard.password) 24 del Richard.password ---> 25 print(Richard.password) <ipython-input-6-a8466536abca> in password(self) 7 @property 8 def password(self): ----> 9 return self.word 10 11 @password.setter #注意寫法 AttributeError: 'Bank' object has no attribute 'word'
@property
所轉換的屬性不可先定義於建構子內¶## 範例
class Bank():
def __init__(self, name):
self.name = name
self.password = 'My password'
@property
def password(self):
return self.password
Richard = Bank('Richard')
print(Richard.password)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-9-eb46ea11c6ad> in <module> 9 return self.password 10 ---> 11 Richard = Bank('Richard') 12 print(Richard.password) <ipython-input-9-eb46ea11c6ad> in __init__(self, name) 4 def __init__(self, name): 5 self.name = name ----> 6 self.password = 'My password' 7 @property 8 def password(self): AttributeError: can't set attribute
@property
修飾器__name__
¶get
與show
.get
印出get
文字即可show
印出show
文字即可__name__
.並建構物件執行此方法.name.py
的檔案中.show
方法.## 範例
class Example():
def __init__(self):
pass
def get(self):
print('get')
def show(self):
print('show')
if(__name__ == '__main__'): #加入此行進行判斷
print('__name__ :', __name__)
e = Example()
e.get()
e.show()
__name__