一.類的定義
1.
class Student(): name = ' ' #定義變數 age = 0 def print_file(self): #定義函式 print('name:' + self.name) print('age:' + str(self.age)) class StudentHomework(): homework_name = ' ' #使用類需要對類進行實體化 student = Student() student.print_file() #呼叫類下面的方法
- 類的最基本作用就是在封裝代碼,
- 類的內部不能呼叫類的方法,類只負責去描述和定義,而不負責執行,運行和呼叫類需要放在類的外部,
- 不推薦在一個模塊中既定義類又使用類,
2.在一個模塊中呼叫另一個模塊的類:
#c2.py中的代碼 from c1 import Student#Student類屬于c1模塊 student = Student()#實體化 student.print_file()#呼叫Student類中的print_file()方法
二.函式與方法的區別
方法是設計層面上的稱謂,更多的是面向物件中的概念;
函式是程式運行的,面向程序的一種稱謂,
沒有必要特別的去區分方法和函式,
變數出現在類中更多稱為資料成員,
三.建構式
1.實體化創造不同的物件,
student1 = Student() student2 = Student() student3 = Student() print(id(student1)) print(id(student2)) print(id(student3)) 結果 #3269808 #3410256 #3410320
2.
def __init__(self):宣告建構式
- 建構式的呼叫是自動進行的
- 我們可以去主動呼叫建構式
- 對建構式來說只能 return None
3.建構式的作用
class Student(): name = '' #定義變數(類變數) age = 0 def __init__(self,name,age): #初始化物件的屬性 name = name age = age student1 = Student('朱可愛',18) print(student1.name,student1.age) #回傳了空字串
回傳空字串的原因:類變數和實體變數的原因,實質上回傳的是類變數
嘗試訪問實體變數時:在實體變數串列里查找,如果沒有會到類變數里面尋找,如果類中沒有會到父類尋找,
模塊中的類似情況:區域變數不會覆寫全域變數
c = 50 def add(x,y) c = x + y #這個c不會覆寫全域變數的c print(c) 結果: 3
50
四.類變數與實體變數
1.定義:
類變數:和類相關的變數(類變數不會受實體變數的影響)
實體變數:和物件相關聯的變數
2.代碼
class Student(): name = 'cute' age = 0 def __init__(self,name,age): self.name = name self.age = age student1 = Student('朱可愛',18) #實體變數 student2 = Student('朱cute',19) print(student1.name) print(student2.name) print(Student.name)#類變數不會受實體變數的影響 結果: #朱可愛 #朱cute #cute#類變數不會受實體變數的影響
五.self與實體方法
1.如果在類中定義實體方法的話,要固定在類中放上self


2.self代表實體而不是類,注意:self不能稱作關鍵字,self可以改為任意值,但是python建議使用self
3.實體方法是實體可以呼叫的方法,和物件實體相關聯,
六.在實體方法中訪問實體變數與類變數
1.在建構式內部訪問實體變數:
class Student(): name = '123' age = 0 sum = 0 def __init__(self,name,age): self.name = name self.age = age print(self.name) #讀取的是self.name,訪問實體變數要加self print(name) #讀取的是形參的name,如果形參串列變成(self,name1,age),則會報錯 student1 = Student('cute',18) 結果: cute cute
2.在實體方法中訪問類變數:
print(Student.sum1) #第一種訪問類變數的方式(類名.變數名)
print(self.__class__.sum1) #第二種訪問類變數的方式
class Student(): name = '123' age = 0 sum1 = 0 def __init__(self,name,age): self.name = name self.age = age print(Student.sum1) #第一種訪問類變數的方式(類名.變數名) print(self.__class__.sum1) #第二種訪問類變數的方式 結果: 0 0
七.類方法
類變數的使用場景:
class Student(): name = '123' age = 0 sum1 = 0 def __init__(self,name,age): self.name = name self.age = age self.__class__.sum1 += 1 print("當前班級人數為" + str(self.__class__.sum1)) student1 = Student('cute',18) student2 = Student('朱cute',18) student3 = Student('朱可愛',18) 結果: #當前班級人數為1 #當前班級人數為2 #當前班級人數為3
1.定義類方法:
@classmethod #裝飾器 def 方法名(cls): #class簡寫,cls代表呼叫類方法的類,cls可以換成別的名字,但是不建議更換 pass
2.類方法里操作類變數:(更改類變數的使用場景的代碼)
class Student(): name = '123' age = 0 sum1 = 0 def __init__(self,name,age): self.name = name self.age = age #self.__class__.sum1 += 1 #print("當前班級人數為" + str(self.__class__.sum1)) @classmethod def plus_sum(cls): cls.sum1 += 1 print(cls.sum1) student1 = Student('cute',18) Student.plus_sum() #呼叫類方法 student2 = Student('朱cute',18) Student.plus_sum() student3 = Student('朱可愛',18) Student.plus_sum()
3.區別:實體方法關聯物件,類方法關聯類本身,
4.可以用物件呼叫類方法:
student1 = Student('cute',18) student1.plus_sum() #可以,但是最好不要,在邏輯上說不通
八.靜態方法
1.定義靜態方法:
@staticmethod #裝飾器 def 方法名(x,y): #沒有指代 pass
2.靜態方法能夠被類和物件呼叫:
@staticmethod #裝飾器 def add(x,y): #沒有指代 print('This is a static method') student1 = Student('cute',18) student1.add(1,2) #被物件呼叫 Student.add(1,2) #被類呼叫 結果: This is a static method This is a static method
3.靜態方法內部可以訪問類變數
@staticmethod #裝飾器 def add(x,y): #沒有指代 print(Student.sum1) #可以訪問類變數 print('This is a static method')
4.靜態方法和類方法都無法訪問實體變數,

5.能用靜態方法的時候都可用類方法替代,最好不要經常使用,與類和物件的關聯性非常弱,和普通的函式幾乎沒有區別,


九. 成員可見性:公開和私有
1.成員需要有一定的呼叫層級,以打分為例:
class Student(): name = '123' age = 0 sum1 = 0 score = 0 def __init__(self,name,age): self.name = name self.age = age self.score = 0 def marking(self,score): if score < 0: #可以對資料進行判斷 score = 0 self.score = score print(self.name + '本次考試分數為' + str(self.score)) student1 = Student('cute',18) student1.marking(100) #利用marking方法打分
2.
不加雙下劃線:公開
加雙下劃線:私有 eg:__add
前后都有雙下劃線:公開,是python內置函式的命名風格,eg:__init__():
3.嘗試從外部訪問私有變數:
class Student(): def __init__(self,name,age): self.name = name self.age = age self.__score = 0 def marking(self,__score): if __score < 0: __score = 0 self.__score = __score print(self.name + '本次考試分數為' + str(self.__score)) student1 = Student('cute',18) student1.marking(100) student1.__score = -1 #沒有報錯 print(student1.__score) 結果: 100 -1
__score不是私有變數嗎?為什么在外部賦值又可以訪問呢?因為,student1.__score = -1 實際上是給sutdent1新添加了一個實體變數,
舉例:如果沒有新添加,是不能在外部訪問私有變數
class Student(): def __init__(self,name,age): self.name = name self.age = age self.__score = 0 def marking(self,__score): if __score < 0: __score = 0 self.__score = __score print(self.name + '本次考試分數為' + str(self.__score)) student1 = Student('cute',18) student2 = Student('朱cute',19) student1.marking(100) student1.__score = -1 #實際上是利用python動態特性,給student1添加了一個新的屬性叫__score, 并不是實體變數中的__score print(student1.__score) #結果:-1 print(student2.__score) #報錯了
通過這種方式可以間接的讀取私有變數:單下劃線 + 類名 + 雙下劃線 + 實體變數名
舉例:
print(student2._Student__score) #讀取到了私有變數,不會報錯 結果:本次考試分數為59
10.繼承
#Filename:cls.py class People(): def __init__(self,name,age): self.name=name self.age=age def sayHello(self): print('Hello!')
import cls class Student(cls.People): pass stu=Student('Tom',18) stu.sayHello()
輸出結果:
Hello!Student類繼承了People類的方法
import cls class Student(cls.People): def __init__(self,name,age,score): self.score=score cls.People.__init__(self,name,age) def sayHello(self): print('Hi!') stu=Student('Tom',18,90) stu.sayHello()
輸出結果:
Hi子類擴展了父類的init()方法,但是呼叫父類的init方法時,必須要傳入self
然后子類又重寫了父類的sayHello()方法
呼叫父類的方法時也可以使用super()
import cls class Student(cls.People): def __init__(self,name,age,score): self.score=score super(Student,self).__init__(name,age) def sayHello(self): print('Hi!') stu=Student('Tom',18,90) stu.sayHello() print(stu.name) print(stu.age)
輸出結果:
Hi Tom 18使用super()可以不用指定父類名稱
11.多繼承
python可以多繼承
class A(): def __init__(self): print('a') class B(): def __init__(self): print('b') class C(A,B): pass c=C()
輸出結果:
a如果父類有相同的方法,子類會優先呼叫先被繼承的類的方法
12.多型
class C(): def say(self): pass class B(C): def say(self): print('B') class A(C): def say(self): print('A') b=B() a=A() b.say() a.say()
輸出結果:
B A轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/198234.html
標籤:Python
上一篇:python筆記04
