zoukankan      html  css  js  c++  java
  • python小练习--函数调用函数,让对象具有能动性

     1 class Box:#定义一个类名为Box,类名后不必有括号,类包含类属性和类方法,这个类没有定义类属性
     2     '''这是一个计算体积的类'''#这是这个类的__doc__属性,执行类后就可以在交互界面输入Box.__doc__查看这行说明文字了
     3     openstate=0
     4     def __init__(self):#这是类的构造函数,当实例化Box后会自动调用这个__init__方法
     5         self.length=0.0 #这是实例属性,在类内访问用self.length,在类外访问用  实例名.length
     6         self.width=0.0
     7         self.height=0.0
     8         self._color='red'       
     9         self.__valum=0.0#双下换线开头的变量表示私有变量,所以他为私有实例属性,只能在类内访问到
    10     def set_color(self,color):
    11         self._color=color    
    12         
    13     def computevalum(self):#定义了一个类方法。
    14         self.__valum=self.length*self.width*self.height
    15         print('长度=',self.length,'宽度=',self.width,'高度=',self.height,'valum=',self.__valum)
    16 
    17     def info_color(self):
    18         #self.set_color(self._color)#在类中,函数调用函数的方式
    19         print('Box的颜色为',self._color)
    20 
    21     def open_box(self):
    22         if Box.openstate==0:
    23             print('打开了Box')
    24             Box.openstate=1
    25         else:
    26             print('Box已经打开了,不能重复打开')
    27 if __name__=='__main__':       
    28     computa=Box() #实例化Box类
    29     computa.length=1
    30     computa.width=2
    31     computa.height=3
    32     computa.computevalum()
    33     computa.set_color ('yellow')
    34     computa.info_color()
    35     computa.open_box()
    36     print('')
    37 
    38     computb=Box()#实例化Box类
    39     computb.length=2
    40     computb.width=2
    41     computb.height=3
    42     computb.computevalum()
    43     computb.set_color ('black')
    44     computb.info_color()
    45     computb.open_box()
  • 相关阅读:
    MVC,MVP,MVVM的区别
    jQuery Mobile 实现苹果滑动删除闹钟功能的几点总结
    AngularJS执行流程详解
    基于angularJS的表单验证练习
    jQuery 中 attr() 和 prop() 方法的区别
    天猫消息盒子的CSS实现
    jQuery事件之传递参数
    浅谈js中的垃圾两种回收机制
    js中的DOM操作汇总
    log4net
  • 原文地址:https://www.cnblogs.com/zhubinglong/p/6942289.html
Copyright © 2011-2022 走看看