class Box:#定义一个类名为Box,类名后不必有括号,类包含类属性和类方法,这个类没有定义类属性
'''这是一个计算体积的类'''#这是这个类的__doc__属性,执行类后就可以在交互界面输入Box.__doc__查看这行说明文字了
openstate=0
def __init__(self):#这是类的构造函数,当实例化Box后会自动调用这个__init__方法
self.length=0.0 #这是实例属性,在类内访问用self.length,在类外访问用 实例名.length
self.width=0.0
self.height=0.0
self._color='red'
self.__valum=0.0#双下换线开头的变量表示私有变量,所以他为私有实例属性,只能在类内访问到
def set_color(self,color):
self._color=color def computevalum(self):#定义了一个类方法。
self.__valum=self.length*self.width*self.height
print('长度=',self.length,'宽度=',self.width,'高度=',self.height,'valum=',self.__valum) def info_color(self):
#self.set_color(self._color)#在类中,函数调用函数的方式
print('Box的颜色为',self._color) def open_box(self):
if Box.openstate==0:
print('打开了Box')
Box.openstate=1
else:
print('Box已经打开了,不能重复打开')
if __name__=='__main__':
computa=Box() #实例化Box类
computa.length=1
computa.width=2
computa.height=3
computa.computevalum()
computa.set_color ('yellow')
computa.info_color()
computa.open_box()
print('') computb=Box()#实例化Box类
computb.length=2
computb.width=2
computb.height=3
computb.computevalum()
computb.set_color ('black')
computb.info_color()
computb.open_box()

最新文章

  1. Python协程:从yield/send到async/await
  2. Linux启动盘-ultraiso
  3. win10 开机黑屏只显示鼠标
  4. Unity3d《Shader篇》Logo闪光特效
  5. 【BZOJ】1135: [POI2009]Lyz
  6. 使用Enitity Framework实现增删改查服务中的一些通用思路
  7. linux tar 解压命令总结
  8. HDOJ 1312题Red and Black
  9. [PWA] 10. Trigger a version update
  10. wpf 类似TextBlock外观的Button的样式
  11. linux source命令与export命令的区别
  12. 搭建多系统yum服务器
  13. Bootstrap3基础 栅格系统 标尺(col-lg/md/sm/xs-1)
  14. BZOJ1012 最大数maxnumber
  15. 英语口语练习系列-C31-图书-谈论事物-白雪歌送武判官归京
  16. 【转】java日志组件介绍(common-logging,log4j,slf4j,logback )
  17. 实现Ajax异步的layui分页
  18. Eclipse添加git插件上传项目到github
  19. CNN中的卷积理解和实例
  20. python类与对象的内置函数大全(BIF)

热门文章

  1. Oracle SQL Developer在进行查询的时候只显示50条数据
  2. CDLinux制作成U盘启动
  3. ArcGIS案例学习笔记3_1
  4. hdoj1010 奇偶剪枝+DFS
  5. myeclipse 10激活,本人已测试过可行
  6. java实现24点游戏代码
  7. ECMAScript6新特性之Array API
  8. TZOJ 1242 求出前m大的数(预处理)
  9. C++ define与const
  10. python作业之修改用户配置文件