标签:聚合 解释器 att 学生 代码 init student 查看 int
class 类名:
属性
方法
__dict__
:查看类中所有内容,并以字典形式返回class Student:
daily = '学习'
examination = '考试'
def __init__(self):
self.n = 'haha'
self.s = 'heihei'
def work(self):
print('上课')
def homework(self):
print('做作业')
类名调用类中的属性
使用__dict__
print(Student.__dict__)
{'__module__': '__main__', '__doc__': '\n 此类是构建学生类\n ', 'daily': '学习', 'examination': '考试', '__init__': <function Student.__init__ at 0x109a08b70>, 'work': <function Student.work at 0x109a08d08>, 'homework': <function Student.homework at 0x109a08d90>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>}
print(Student.__dict__.['daily'])
学习
万能的点 .(增删改查)
print(Student.daily) #查
Student.cloth = '蓝色' #增
Student.examination = '不考试' #改
del Student.examination #删
类名调用类中的方法
类名.方法名() (但工作中一般不会这样使用)
Student.work()
类名() :实例化一个对象,它的本质是每个类中的一个特殊函数
class Student:
daily = '学习'
examination = '考试'
def __init__(self,n,a,h):
self.name = n
self.age = a
self.habby = h
def work(self,c):
self.cloth = c
print(f'{self.name}上课')
def homework(self):
print('做作业')
xiaohei = Student('小黑',18,'打球')
xiaoming = Student('小明',18,'打球')
print(xiaohei)#<__main__.Student object at 0x1031896d8>
print(xiaoming)#<__main__.Student object at 0x4565896d8>
__init__
方法,并且将对象空间传给self参数__init__
方法,并将对象空间封装其属性class Student:
daily = '学习'
examination = '考试'
def __init__(self):
self.n = 'haha'
self.s = 'heihei'
def work(self):
print('上课')
def homework(self):
print('做作业')
obj = Student()
obj.n = 'hahei' #改
obj.age = 18 #增
print(obj.s) #查
del obj.n #删
class Student:
'''
此类是构建学生类
'''
daily = '学习'
examination = '考试'
def __init__(self,n,a,h):
self.name = n
self.age = a
self.habby = h
def work(self):
print('每天要上课')
def homework(self):
print('家庭作业')
xiaohei = Student('小黑',18,'打球')
xiaohei.work()
标签:聚合 解释器 att 学生 代码 init student 查看 int
原文地址:https://www.cnblogs.com/xiaohei-chen/p/12059224.html