面向對象編程,是一種編程方式,這種編程方式需要使用“對象”來實現
對象的特徵
- 世間萬物皆對象
- 每個對象都是唯一的
- 對象具有屬性和行爲(對象的行爲包括具有的功能及具體的實現)
- 對象具有狀態
- 對象分爲類對象和實例對象兩大類
類對象:具有相同屬性和行爲的實例對象的抽象
類對象是實例對象的模板,實例對象是由類對象創建出來的
面向對象編程的大體步驟
1.抽象出類對象
2.抽象出類對象的屬性
3.抽象出類對象的行爲(方法)
4.根據類對象創建實例對象
5.通過實例對象訪問屬性和方法
取名規則
- 大駝峯規則:
每個單詞首字母大寫 - 小駝峯規則:
第一個單詞首字母小寫,其他大寫
定義類對象的語法格式:
class 類名(object):
#屬性與方法
(object)表示該類對象繼承自python內置的類對象object,python中所有的類對象都繼承自一個統一的基類:object
例:class SomeClass(object):
....... pass
#初識屬性:
class Cat(object):
def __init__(self,breed,name,age,health):
self.breed = breed
self.name = name
self.age = age
self.health = health
#初識類的方法:
def run(self):
print("a")
def miao(self):
print("b")
def eat(self):
print("c")
類的方法與函數的區別:
方法就是定義在類對象中的函數,方法與函數的區別在於:
1.定義方法時。方法的第一個形參表示調用該方法的實例對象,第一個形參的名字通常是self。也可以是其他名稱。
2.調用方法時,系統自動將調用該方法的實例對象作爲實參傳遞給第一個形參,第一個實參會傳遞給第二個形參,第二個實參會傳遞給第三個形參,以此類推
根據類對象創建實例對象的語法格式爲:
類名([實參])
#根據類對象創建實例對象
cat = Cat("波斯貓","喵喵",1,"很好")
print(cat.breed)
>>>波斯貓
cat.run()
>>>a
實例屬性
實例屬性指的是實例對象所綁定的屬性
綁定實例屬性(給實例對象綁定屬性)的方式有兩種:
1.在類對象的內部(方法中)
語法格式:self.屬性名 = 屬性值
2.在類對象的外部
python是動態語言,所以,在實例對象創建之後,可以對其動態地綁定屬性。
語法格式:實例對象.屬性名 = 屬性值
如果指定名稱的實例屬性已經存在,則是對實例屬性進行修改
訪問實例屬性的方式有兩種:
1.在類對象的內部(方法中)
語法格式:self.屬性名
2.在類對象的外部
語法格式:實例對象.屬性名
class MyClass(object):
def __init__(self):
self.ia1 = 18
def do_sth1(self):
print(self.ia1)
def do_sth2(self):
print(self.ia2)
def do_another(self):
self.ia2 = 56
def do_sth3(self):
print(self.ia3)
mc = MyClass()
mc.do_sth1()
#mc.do_sth2()(不能調用)
>>>18
mc.do_another()#先調用這個函數
mc.do_sth2()
>>>56
mc.do_sth3()
沒有定義,出錯
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-13-c24edc76ceaa> in <module>()
----> 1 mc.do_sth3()
<ipython-input-11-2614780adaa1> in do_sth3(self)
9 self.ia2 = 56
10 def do_sth3(self):
---> 11 print(self.ia3)
12 mc = MyClass()
13 mc.do_sth1()
AttributeError: 'MyClass' object has no attribute 'ia3'
mc.ia3 = 66#外部賦值
mc.do_sth3()
>>>66
屬性值是跟着對象的
mc2 = MyClass()
print(mc2.ia3)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-15-8b6c73677368> in <module>()
1 mc2 = MyClass()
----> 2 print(mc2.ia3)
AttributeError: 'MyClass' object has no attribute 'ia3'
#實例對象.__dict__(可獲得該實例對象所綁定的所有屬性及其值得字典)
mc.__dict__
>>>{'ia1': 18, 'ia2': 56, 'ia3': 66}
mc2.__dict__
>>>{'ia1': 18}
dir(mc)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'do_another', 'do_sth1', 'do_sth2', 'do_sth3', 'ia1', 'ia2', 'ia3']
dir(mc2)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'do_another', 'do_sth1', 'do_sth2', 'do_sth3', 'ia1']