python 面向物件-例項化(物件)
阿新 • • 發佈:2020-08-05
1、建構函式__init__()
#coding=utf-8 class Student(): name = "" age = 0 def __init__(self): #建構函式 pass def do_homework(self): print(self.name +"do homework") student1 = Student() student2 = Student() student3 = Student()
例項化的時候,python 會自動的呼叫建構函式,不需要去顯示呼叫,如果非要調,也可以
#coding=utf-8 class Student(): name = "" age = 0 def __init__(self): #建構函式 pass def do_homework(self): print(self.name +"do homework") student1 = Student() a = student1.__init__() print(type(a)) # Project/python_ToolCodes/test6.py" # <type 'NoneType'>
這個說明 顯示呼叫建構函式沒有什麼特別的,跟普通函式沒區別,且無返回值。如果非要返回
#coding=utf-8 class Student(): name = "" age = 0 def __init__(self): #建構函式 return "111" def do_homework(self): print(self.name +"do homework") student1 = Student() a = student1.__init__() print(type(a)) # Project/python_ToolCodes/test6.py" # Traceback (most recent call last):# File "/Users/anson/Documents/Project/python_ToolCodes/test6.py", line 13, in <module> # student1 = Student() # TypeError: __init__() should return None
這就跟Java 一樣了,建構函式無返回值,如果非要強制返回,那就報錯了,因為python 和Java一樣 不允許在建構函式中返回
這樣也算是啥也不返回,和不寫return 是一樣的
#coding=utf-8 class Student(): name = "" age = 0 def __init__(self): #建構函式 return def do_homework(self): print(self.name +"do homework") student1 = Student() a = student1.__init__() print(type(a)) # Project/python_ToolCodes/test6.py" # <type 'NoneType'>
2、帶參建構函式__init__(self,name,age):用來初始化物件的屬性
#coding=utf-8 class Student(): name = "" age = 0 def __init__(self,name,age): #建構函式 self.name = name self.age = age def do_homework(self): print(self.name +"do homework") student1 = Student("ansonw",26) print("name:"+student1.name) print("age:"+str(student1.age)) # Project/python_ToolCodes/test6.py" # name:ansonw # age:26
3、類變數和例項變數(關於類變數的使用方法,似乎與Java 不同)
Student.name 和 student1.name
#coding=utf-8 class Student(): name = "kiki" age = 0 def __init__(self,name,age): #建構函式 self.name = name self.age = age def do_homework(self): print(self.name +"do homework") student1 = Student("ansonw",26) print("例項變數=>name:"+student1.name) print("類變數=>name:"+Student.name) # 例項變數=>name:ansonw # 類變數=>name:kiki
但是,類是不需要名字和年齡的,所以這裡類有名字為"kiki" 是不合理的
4、__dict__python內建變數列印,物件的內容和類的內容
student1.__dict__
物件:
#coding=utf-8 class Student(): name = "" age = 0 def __init__(self,name,age): #建構函式 self.name = name self.age = age def do_homework(self): print(self.name +"do homework") student1 = Student("ansonw",26) print(student1.__dict__) # [Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" # {'age': 26, 'name': 'ansonw'}
類:
#coding=utf-8 class Student(): name = "" age = 0 def __init__(self,name,age): #建構函式 self.name = name self.age = age def do_homework(self): print(self.name +"do homework") student1 = Student("ansonw",26) print(Student.__dict__) #[Running] python -u "/Users/anson/Documents/Project/python_ToolCodes/test6.py" #{'__module__': '__main__', 'do_homework': <function do_homework at 0x10ab0d758>, 'name': '', 'age': 0, '__doc__': None, '__init__': <function __init__ at 0x10ab0d140>}
5、python 中的self 與this
- Java 中的用this 來指代當前物件,python中可以用this,但是建議使用self
- 類方法定義時,必須要帶上self
-
def __init__(self,name,age): #建構函式 self.name = name self.age = age def do_homework(self): print(self.name +"do homework")
- 類方法倍呼叫時,不需要帶self,因為python自動給我們加上了這個入參
-
student1.do_homework()