您好,登錄后才能下訂單哦!
對象:特征與技能的集合體
類:一系列對象相似的特征與技能的集合體
即動物類:
特征:鼻子,眼睛,耳朵,嘴巴
技能:吃,跑
在現實世界中,肯定是現有對象,然后人類根據對象的一些共有特征,對其進行分類。
在編程中,需要先定義類,然后實例化產生對象
"現實世界中,先有對象,后產生類"
對象1:張三
特征:
學校='my_school'
姓名=張三
性別=男
年齡=18
技能:
學習
吃飯
睡覺
對象2:李四
特征:
學校='my_school'
姓名=李四
性別=女
年齡=38
技能:
學習
吃飯
睡覺
現實中的學生類
相似的特征:
學校='my_school'
相似的技能:
學習
吃飯
睡覺
"程序中,先有類,后使用類產生對象"
#在Python中程序中的類用class關鍵字定義,而在程序中特征用變量標識,技能用函數標識,因而類中最常見的無非是:變量和函數的定義
#定義類
class Student:
school='my_school'
def learn(self):
print('is learning')
def eat(self):
print('is eating')
def sleep(self):
print('is sleeping')
#后產生對象
s1 = Student()
s2 = Student()
print(s1)
print(s2)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test2/ww/ee.py
<__main__.Student object at 0x000001AF8F10EBA8>
<__main__.Student object at 0x000001AF8F10EB70>
Process finished with exit code 0
"
注意:
1.類中可以有任意python代碼,這些代碼是在類定義階段就會執行,因而會產生新的名稱空間,用來存放類的變量名與函數名,可以通過Student.__dict__查看
2.類中定義的名字,都是類的屬性,可以通過Student.來訪問屬性
3.對于經典類,我們可通過__dict__產生的字典操作類名稱空間的名字,但新式類有限
"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class Student:
school = 'my_school'
print(school) # 驗證類中的代碼,在定義階段就會執行,因而會產生新的名稱空間
def learn(self):
print('is learning')
def eat(self):
print('is eating')
def sleep(self):
print('is sleeping')
print("Student.__dict__:",Student.__dict__) # 驗證可通過Student.__dict__查看類名稱空間中的變量與函數名
print("Student.school:",Student.school) #驗證可通過.訪問類中的屬性
print("Student.__dict__['school']",Student.__dict__["school"])# 驗證可通過訪問字典方式訪問類中屬性
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
my_school
Student.__dict__: {'__module__': '__main__', 'school': 'my_school', 'learn': <function Student.learn at 0x000001F9B0CD1C80>, 'eat': <function Student.eat at 0x000001F9B0CD1D90>, 'sleep': <function Student.sleep at 0x000001F9B0CD1D08>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}
Student.school: my_school
Student.__dict__['school'] my_school
Process finished with exit code 0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class Student:
school = 'my_school' #數據屬性
def learn(self): #函數屬性
print('is learning')
def eat(self): #函數屬性
print('is eating')
def sleep(self):
print('is sleeping')
#查看類的名稱空間
print("查看類的名稱空間Student.__dict__:",Student.__dict__)
print("查看類的名稱空間Student.__dict__['school']:",Student.__dict__["school"])
#查詢屬性
print("查詢Student.school",Student.school)
print("查詢Student.__dict__['school']:",Student.__dict__["school"])
#增加屬性
Student.country='China'
print("增加后的Student.__dict__['country']:",Student.__dict__["country"])
#刪除屬性
del Student.country
print("刪除后的Student.__dict__:",Student.__dict__)
#修改屬性
Student.school="new_shcool"
print("修改后的Student.__dict__['school']:",Student.__dict__["school"])
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
查看類的名稱空間Student.__dict__: {'__module__': '__main__', 'school': 'my_school', 'learn': <function Student.learn at 0x0000021E0D601D90>, 'eat': <function Student.eat at 0x0000021E0D601D08>, 'sleep': <function Student.sleep at 0x0000021E0D60B158>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}
查看類的名稱空間Student.__dict__['school']: my_school
查詢Student.school my_school
查詢Student.__dict__['school']: my_school
增加后的Student.__dict__['country']: China
刪除后的Student.__dict__: {'__module__': '__main__', 'school': 'my_school', 'learn': <function Student.learn at 0x0000021E0D601D90>, 'eat': <function Student.eat at 0x0000021E0D601D08>, 'sleep': <function Student.sleep at 0x0000021E0D60B158>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}
修改后的Student.__dict__['school']: new_shcool
Process finished with exit code 0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class Student:
school = 'my_school' #數據屬性,屬于類的s1.school會先到s1.__dict__中找,找不到,去Student類中找
def learn(self): #函數屬性
print('is learning')
def eat(self): #函數屬性
print('is eating')
def sleep(self):
print('is sleeping')
s1 = Student()
s2= Student()
# 對象的屬性是專屬于特定對象的屬性,不同對象之間互不影響
#查看對象的名稱空間
#注意:這里為空,因為school變量是綁定給Student類的,給所有對象使用,不是單獨給某一個對象使用。函數也是綁定給某個對象的,后面會詳細介紹
print("查看類的名稱空間s1.__dict__:",s1.__dict__)
print("查看類的名稱空間s2.__dict__:",s2.__dict__)
#查詢屬性
print("查詢s1.school",s1.school)
print("查詢s2.school",s2.school)
#增加屬性
#增加了屬于該對象的屬性
s1.country='China'
s2.country='America'
print("增加后的s1.__dict__['country']:",s1.__dict__["country"])
print("增加后的s1.country:",s1.country)
print("增加后的s2.country:",s2.country)
#刪除屬性
del s1.country
print("刪除后的s1.__dict__:",s1.__dict__)
print("刪除后的s2.__dict__:",s2.__dict__)
#修改屬性
#注意:這里并沒有真正修改類中的scholl變量,而是為s1對象增加了一個school變量
s1.school="new_shcool"
print("修改后的s1.school",s1.school)
print("修改后的s2.school",s2.school)
print("修改后的s1.__dict__:",s1.__dict__)
print("修改后的s2.__dict__:",s2.__dict__)
# 要想真正修改類中的school變量,需要使用Student.school="new_school",并且修改了,對所有對象起作用
Student.school="new_school_class"
del s1.school #因為之前為s1對象增加了school變量,所以需要刪除掉,否則s1.school會先找s1.__dict__中的變量,找不到再去類中找
print("修改Student.school后的s1.school",s1.school)
print("修改Student.school后的s2.school",s2.school)
print("修改Student.school后的s1.__dict__:",s1.__dict__)
print("修改Student.school后的s2.__dict__:",s2.__dict__)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
查看類的名稱空間s1.__dict__: {}
查看類的名稱空間s2.__dict__: {}
查詢s1.school my_school
查詢s2.school my_school
增加后的s1.__dict__['country']: China
增加后的s1.country: China
增加后的s2.country: America
刪除后的s1.__dict__: {}
刪除后的s2.__dict__: {'country': 'America'}
修改后的s1.school new_shcool
修改后的s2.school my_school
修改后的s1.__dict__: {'school': 'new_shcool'}
修改后的s2.__dict__: {'country': 'America'}
修改Student.school后的s1.school new_school_class
修改Student.school后的s2.school new_school_class
修改Student.school后的s1.__dict__: {}
修改Student.school后的s2.__dict__: {'country': 'America'}
Process finished with exit code 0
"該方法是在對象產生后才會執行,只用來為對象進行初始化操作,可以是任意代碼,但不能有返回值"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
class Student:
school = 'my_school'
def __init__(self,name,sex,age):# 屬于對象的變量,在s1.__dict__中可查看到
self.name=name
self.sex=sex
self.age=age
def learn(self): #函數屬性
print('is learning')
def eat(self): #函數屬性
print('is eating')
def sleep(self):
print('is sleeping')
#加上__init__方法后,實例化的步驟
# 1.產生一個空對象s1
# 2.Student.__init__(s1,'vita','女',27)
s1=Student
Student.__init__(s1,'lyly','女',28)
s2=Student('vita','女',27)
s3=Student('chaochao','男',30)
#查
print("s1.__dict__",s1.__dict__)
print("s2.__dict__",s2.__dict__)
print("s3.__dict__",s3.__dict__)
print("查詢s2.name",s2.name)#等同于s2.__dict__["name"]
#改
s2.name="vita_new" #等同于s2.__dict__["name"]="vita_new"
print("修改后的s2.__dict__",s2.__dict__)
print("修改后的s2.name",s2.name)
#刪除
del s2.name #等同于s2.__dict__.pop("course")
print("刪除后的s2.__dict__",s2.__dict__)
#增加
s2.class_name="python" #等同于s2.__dict__["class_name"]="python"
print("增加后的s2.__dict__",s2.__dict__)
#是否對s3對象有所影響
print("操作后的s3.__dict__",s3.__dict__)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
s1.__dict__ {'__module__': '__main__', 'school': 'my_school', '__init__': <function Student.__init__ at 0x0000018C0ED71C80>, 'learn': <function Student.learn at 0x0000018C0ED71D90>, 'eat': <function Student.eat at 0x0000018C0ED71D08>, 'sleep': <function Student.sleep at 0x0000018C0ED7B158>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None, 'name': 'lyly', 'sex': '女', 'age': 28}
s2.__dict__ {'name': 'vita', 'sex': '女', 'age': 27}
s3.__dict__ {'name': 'chaochao', 'sex': '男', 'age': 30}
查詢s2.name vita
修改后的s2.__dict__ {'name': 'vita_new', 'sex': '女', 'age': 27}
修改后的s2.name vita_new
刪除后的s2.__dict__ {'sex': '女', 'age': 27}
增加后的s2.__dict__ {'sex': '女', 'age': 27, 'class_name': 'python'}
操作后的s3.__dict__ {'name': 'chaochao', 'sex': '男', 'age': 30}
Process finished with exit code 0
1.站的角度不同,定義出的類是不同的
2.現實中的類并不完全等于程序中的類,比如現實中公司類,在程序中需要拆分為部門類,業務類
3.有時為了編程需求,程序中可能出現現實中不存在的類,但這在程序中是很普遍的,需要把現實與程序分別開來
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。