目錄
一、編程思想?
二、類與對(duì)象?
三、類的創(chuàng)建?
四、對(duì)象的創(chuàng)建?
五、類屬性、類方法、靜態(tài)方法?
六、動(dòng)態(tài)綁定屬性和方法?
七、知識(shí)點(diǎn)總結(jié)
八、面向?qū)ο蟮娜筇卣?/p>
1.封裝?
2.繼承?
3.多態(tài)
九、方法重寫(xiě)
十、object類?
十一、特殊方法和特殊屬性
1.dict/len/add?
2.new?/?init
?十二、類的賦值與淺拷貝
十三、知識(shí)點(diǎn)總結(jié)?
一、編程思想
二、類與對(duì)象?
python中一切皆對(duì)象
三、類的創(chuàng)建
類的名稱由一個(gè)或多個(gè)單詞組成,每個(gè)單詞的首字母大寫(xiě),其余小寫(xiě)
類是對(duì)象,開(kāi)辟內(nèi)存空間
這里self當(dāng)然可以換為其他單詞 ,但是必須存在這樣一個(gè)單詞,python默認(rèn)就是self
在類里定義的函數(shù)稱為實(shí)例方法,在類之外定義的函數(shù)稱為函數(shù)。
四、對(duì)象的創(chuàng)建
class Student:# Student稱為類的名稱
native_pace='吉林'#直接寫(xiě)在類里面的變量,稱為類屬性
def __init__(self,name,age):#name,age是實(shí)例屬性
self.name=name
self.age=age
#實(shí)例方法
def eat(self):
print('學(xué)生在吃飯')
#靜態(tài)方法,使用@staticmethod
@staticmethod
def method():
print('靜態(tài)方法中不允許寫(xiě)self')
#類方法,@classmethod
@classmethod
def cm(cls):
print('類方法中傳cls')
#在類之外定義的稱為函數(shù),在類之內(nèi)定義的稱為方法
stu1=Student('張三',20)
stu1.eat() #對(duì)象名.方法名
stu1.cm()
stu1.method()
print(stu1.name,stu1.age)
# 學(xué)生在吃飯
# 類方法中傳cls
# 靜態(tài)方法中不允許寫(xiě)self
# 張三 20
Student.eat(stu1) #和上面的調(diào)用方法功能一樣
# 學(xué)生在吃飯
五、類屬性、類方法、靜態(tài)方法
print(Student.native_pace)#吉林
stu1=Student('張三',20)
stu2=Student('李四',30)
print(stu1.native_pace)
print(stu2.native_pace)
# 吉林
# 吉林
Student.native_pace='天津'
print(stu1.native_pace)
print(stu2.native_pace)
# 天津
# 天津
Student.cm()
Student.method()
# 類方法中傳cls
# 靜態(tài)方法中不允許寫(xiě)self
六、動(dòng)態(tài)綁定屬性和方法
class Student:
def __init__(self,name,age):
self.name=name
self.age=age
def eat(self):
print(self.name+'在吃飯')
stu1=Student('張三',30)
stu2=Student('李四',40)
stu2.gender='男' #動(dòng)態(tài)綁定stu2屬性
print(stu1.name,stu1.age)
print(stu2.name,stu2.age,stu2.gender)
# 張三 30
# 李四 40 男
def show():
print('定義在類之外,函數(shù)')
stu1.show=show #動(dòng)態(tài)綁定stu1的方法
stu1.show()
七、知識(shí)點(diǎn)總結(jié)
八、面向?qū)ο蟮娜筇卣?/h3>
1.封裝?
class Student:
def __init__(self, name, age):
self.name = name
self.__age = age
def show(self): #可讓外部對(duì)象通過(guò)調(diào)用成員方法來(lái)進(jìn)行訪問(wèn)
print(self.name, self.__age)
stu = Student('張三', 20)
stu.show()
# 在類的外部使用name和age
print(stu.name)
# print(stu.__age) print(stu.__age)AttributeError: 'Student' object has no attribute 'name'
print(dir(stu))
print(stu._Student__age) # 在類的外部強(qiáng)制訪問(wèn),雖然說(shuō)私有屬性外部沒(méi)法訪問(wèn),但是也不是不能訪問(wèn),只是自覺(jué)不使用進(jìn)行訪問(wèn)
# 20
2.繼承
class Person(object):
def __init__(self,name,age):
self.name=name
self.age=age
def info(self):
print('姓名:{0},年齡:{1}'.format(self.name,self.age))
#定義子類
class Student(Person):
def __init__(self,name,age,score):
super().__init__(name,age)
self.score=score
#測(cè)試
stu=Student('Jack',20,'1001')
stu.info()
# 姓名:Jack,年齡:20
3.多態(tài)
?
九、方法重寫(xiě)
?java就是靜態(tài)語(yǔ)言,python是動(dòng)態(tài)語(yǔ)言,靜態(tài)語(yǔ)言實(shí)現(xiàn)多態(tài)必須明確繼承關(guān)系,而動(dòng)態(tài)語(yǔ)言只關(guān)心你是否具有這個(gè)方法
?
class Animal(object):
def eat(self):
print('動(dòng)物吃')
class Dog(Animal):
def eat(self): #重寫(xiě)父類Animal方法
print('狗吃')
class Cat(Animal):
def eat(self): #重寫(xiě)父類Animal方法
print('貓吃')
class Person:
def eat(self): #重寫(xiě)父類object方法
print('人吃')
def fun(a):#定義一個(gè)函數(shù),來(lái)調(diào)用eat方法,因此只要類中有eat方法,就可以被調(diào)用
a.eat()
fun(Dog())
fun(Cat())
fun(Animal())
fun(Person())
# 狗吃
# 貓吃
# 人吃
class Person(object):
def __init__(self, name, age):
self.name = name
self.age = age
def info(self):
print(self.name, self.age)
class Student(Person):
def __init__(self, name, age, stu_no):
super().__init__(name, age)
self.stu_no = stu_no
# 會(huì)發(fā)現(xiàn)父類中也有info方法,但是子類Student在調(diào)用時(shí)只會(huì)輸出name,age,
# 無(wú)法輸出子類的stu_no,因此想著繼承父類的屬性,但是也要有子類的屬性,于是重寫(xiě)info方法
def info(self):
super().info() #這個(gè)是為了繼承父類的name,age的屬性,不寫(xiě)就繼承不了
print(self.stu_no)
class Teacher(Person):
def __init__(self, name, age, teachofyear):
super().__init__(name, age)
self.teachofyear = teachofyear
stu = Student('張三', 20, '10010')
stu.info()
# 張三 20
# 10010
十、object類?
class Student: #沒(méi)寫(xiě)繼承,默認(rèn)繼承object類
pass
stu=Student()
print(dir(stu))
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-602853.html
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):#相當(dāng)于重寫(xiě)了__str__方法,因?yàn)槔^承的object中也有這個(gè)方法
return '我的名字是{0},今年{1}歲了'.format(self.name, self.age)
stu = Student('張三', 20)
print(dir(stu))
# ['__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__']
print(stu) #默認(rèn)會(huì)調(diào)用__str__這樣的方法,
# <__main__.Student object at 0x0000016E05C38400>
# 我的名字是張三,今年20歲了 #默認(rèn)調(diào)用__str__()這樣的方法,重寫(xiě)了
十一、特殊方法和特殊屬性
1.dict/len/add
a = 20
b = 100
c = a + b
d = a.__add__(b)
print(c) # 120
print(d) # 120 #從此可以看出,ab相加的原理就是調(diào)用了本身的__add__方法,如果本身沒(méi)有add方法,d也不會(huì)有結(jié)果,如下面的例子
class Student:
def __init__(self, name):
self.name = name
def __add__(self, other):
return self.name + other.name
def __len__(self):
return len(self.name)
stu1 = Student('張三')
stu2 = Student('李四')
# print(stu1+stu2)TypeError: unsupported operand type(s) for +: 'Student' and 'Student'
print(stu1 + stu2) # 張三李四
# 實(shí)現(xiàn)了兩個(gè)對(duì)象的加法運(yùn)算,在類中編寫(xiě)了add特殊的方法,沒(méi)有add方法,這兩個(gè)就不能相加
s=stu1.__add__(stu2) #當(dāng)然這樣寫(xiě)也必須是在類中有add方法,如果把Student類中的add方法去除,一樣不對(duì)
print(s)
list=[1,2,3,4]
print(len(list))
print(list.__len__())#可以發(fā)現(xiàn)列表自帶len的內(nèi)置方法
print(len(stu1))
# TypeError: object of type 'Student' has no len() 因?yàn)轭愔袥](méi)寫(xiě)len方法,所以報(bào)錯(cuò)
# 2 在類中編寫(xiě)了len特殊的方法,return len(self.name)
2.new?/?init
class Person(object):
def __new__(cls, *args, **kwargs): #創(chuàng)建對(duì)象
print('__new__被調(diào)用執(zhí)行了,cls的id值為{0}'.format(id(cls))) #4864
obj=super().__new__(cls)
print('創(chuàng)建的對(duì)象的id為:{0}'.format(id(obj))) #7264
return obj
def __init__(self,name,age): #對(duì)創(chuàng)建的對(duì)象初始化
self.name=name
self.age=age
print(' _init__被調(diào)用了,self的id為:{0}'.format(id(self))) #7264
print('object id:{0}'.format(id(object))) #4048
print('Person id:{0}'.format(id(Person))) #4864
p1=Person('三',20)
print('p1 id:{0}'.format(id(p1))) #7264
# object id:140709592514048
# Person id:2701662344864
# __new__被調(diào)用執(zhí)行了,cls的id值為2701662344864
# 創(chuàng)建的對(duì)象的id為:2701663637264
# _init__被調(diào)用了,self的id為:2701663637264
# p1 id:2701663637264
十二、類的賦值與淺拷貝
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-602853.html
#類對(duì)象的賦值操作,形成兩個(gè)變量,實(shí)際上還是指向同一個(gè)對(duì)象
cpu1=CPU()
cpu2=cpu1
print(cpu1)
print(cpu2)
# <__main__.CPU object at 0x0000021A25626FD0>
# <__main__.CPU object at 0x0000021A25626FD0>
disk=Disk()
computer=Computer(cpu1,disk)
import copy
computer2=copy.copy(computer) #淺拷貝,對(duì)象包含的子對(duì)象內(nèi)容不拷貝
computer3=copy.deepcopy(computer) #深拷貝,遞歸拷貝對(duì)象中包含的子對(duì)象
print(computer,computer.cpu,computer.disk)
print(computer2,computer2.cpu,computer2.disk)
print(computer3,computer3.cpu,computer3.disk)
# <__main__.Computer object at 0x000002281D258F70> <__main__.CPU object at 0x000002281D258FD0> <__main__.Disk object at 0x000002281D258FA0>
# <__main__.Computer object at 0x000002281D258820> <__main__.CPU object at 0x000002281D258FD0> <__main__.Disk object at 0x000002281D258FA0>
# <__main__.Computer object at 0x000002281D258730> <__main__.CPU object at 0x000002281D258430> <__main__.Disk object at 0x000002281D258460>
十三、知識(shí)點(diǎn)總結(jié)
到了這里,關(guān)于Python補(bǔ)充筆記4-面向?qū)ο蟮奈恼戮徒榻B完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!