码迷,mamicode.com
首页 > 其他好文 > 详细

如何使用类

时间:2018-09-11 19:39:02      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:obj   sunny   func   def   执行   产生   函数   sel   module   

类在定义阶段已经能执行

class luffystudent:
school=‘luffycity‘
def learn(self):
print(‘is learning‘)
def eat(self):
print(‘is eating‘)
def sleep(self):
print(‘is sleeping‘)

》》》===run===

查看类的名称空间,print(类名.__dict__)

print(luffystudent.__dict__)

{‘__module__‘: ‘__main__‘, ‘school‘: ‘luffycity‘, ‘learn‘: <function luffystudent.learn at 0x00000000021C97B8>, ‘eat‘: <function luffystudent.eat at 0x00000000021C9840>, ‘sleep‘: <function luffystudent.sleep at 0x00000000021C98C8>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘luffystudent‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘luffystudent‘ objects>, ‘__doc__‘: None}

 

类内部定义的变量是类的数据属性

类内部定义的函数是类的函数属性

 

#查
#print(luffystudent.school)
#print(luffystudent.learn)

#增
print(luffystudent.__dict__[‘school‘])
luffystudent.county=‘china‘
print(luffystudent.__dict__)

#删
# del luffystudent.county
# print(luffystudent.__dict__)

#改
luffystudent.school=‘LuffyCity‘
print(luffystudent.__dict__)

如何使用类
#定义类
class luffystudent:
def __init__(self,name,sex,age):
self.name=name
self.sex=sex
self.age=age


school=‘luffycity‘
def learn(self):
print(‘is learning‘)
def eat(self):
print(‘is eating‘)
def sleep(self):
print(‘is sleeping‘)


#后产生对象
stu1=luffystudent(‘sunny‘,‘nan‘,‘28‘)
#实例化步骤
#1.创建一个空对象
#2.luffystudent.__init__(stu1,‘sunny‘,‘nan‘,‘28‘)


# print(luffystudent.__init__)
# print(stu1.__dict__)
# print(stu1.name)

sty2=luffystudent(‘alex‘,‘nan‘,35)
print(sty2.__dict__) #sty2名称空间,键是属性名,值为属性值

如何使用类

标签:obj   sunny   func   def   执行   产生   函数   sel   module   

原文地址:https://www.cnblogs.com/sunny666/p/9629554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!