Python 3.6 类属性
来源:互联网 发布:下载永安期货软件 编辑:程序博客网 时间:2024/05/16 02:34
# -----------------------# __Author : tyran# __Date : 17-11-9# -----------------------"""Python 类 类方法 静态方法 类属性"""class Base: # 静态成员变量(其实我忘了Python里是不是叫这个名字),这个变量是属于类的,所有的类对象共享(如果有好几个对象,大家用的是同一个) # 第二点,静态变量在成员内部是不能直接通过self调用的,Base.static_member和self.static_member可不是同一个变量 # 第三点,不要把普通的成员变量和静态变量搞成一个名字,这里面会有问题,因为Python的高度自由的语法(我觉得这个是因为c)使用的时候会有点误区 static_member = 'I am Static Member' # 构造方法 ----------->self其实就是指的对象 def __init__(self, name, age): print('This is Base __init__') self.name = name self.age = age # 普通方法(公有方法) --------->需要先创建类对象,才能在外部使用 def public_func1(self): print(self.name, self.age) # 静态方法 ------>不需要创建类对象,直接可以通过类去调用 @staticmethod def static_func1(): # 他不需要self print('This is Static Method') # 类方法 ------>不需要创建类对象,直接可以通过类去调用,同样因为cls是类名,所以他可以调用静态变量哦 @classmethod def class_func1(cls): # cls == class,函数传入的其实是一个类名,通过类去调用的时候会将该类自动填充这个cls print('This is Class Method') print(cls.static_member)# 静态方法调用Base.static_func1()# 类方法调用Base.class_func1()# 普通方法调用base = Base('aaa', 21)base.public_func1()# --------总结1------------"""乍一看,静态方法和类方法差不多么,如果差不多为什么会有两种?其实关键就在于cls。我们发现静态方法和类方法是没有self传入的,类方法会把cls传入方法,在方法内就可以调用cls的成员,注意是类成员,也就是静态变量,和函数。那有朋友说了,我给静态方法加一个形参,然后通过形参进行内部调用不可以么?当然可以,但是请注意,用户传什么可是不可控的,等于需要在内部再做容错机制,这就比较麻烦了。"""# 类属性class Base1: def __init__(self, name, age): print('This is Base1 __init__') self.name = name self.age = age self.money = 999 # 这里开始就是属性创建,作用么。。。。其实我也没看出来有啥作用,先理解为,对于内部私有变量访问和设置吧 # 其实没有什么新的内容,对比看一下应该就能理解了 @property def func1(self): print('name : ' + self.name + ' age : ' + str(self.age) + ' money : ' + str(self.money)) return 666 @func1.setter def func1(self, add_money): self.money += add_money print('{0} 发财啦! {0} has ${1}'.format(self.name, self.money)) @func1.deleter def func1(self): self.money = 0 print('{0} 破产了! 他还剩余${1}'.format(self.name, self.money)) # -------------------第二种写法 def func2_getting(self): print('name : ' + self.name + ' age : ' + str(self.age) + ' money : ' + str(self.money)) return 666 def func2_setting(self, add_money): self.money += add_money print('{0} 发财啦! {0} has ${1}'.format(self.name, self.money)) def func2_delete(self): self.money = 0 print('{0} 破产了! 他还剩余${1}'.format(self.name, self.money)) func2_property = property(fget=func2_getting, fset=func2_setting, fdel=func2_delete)base1 = Base1('tom', 18)print(base1.func1) # 调用@property标注的函数,记得不能加()base1.func1 = 1 # 调用@func1.setter标注的函数,函数参数就是等号右边的部分,这里有个问题就是参数只能是一个,我还没看到什么办法添加两del base1.func1 # 调用@func1.deleter标注的函数# 调用方法可以看下,一样的。。。print(base1.func2_property)base1.func2_property = 1del base1.func2_property
阅读全文
0 0
- Python 3.6 类属性
- Python类属性,实例属性
- python类-属性
- Python 类属性介绍
- Python 类属性
- Python--类的属性
- python类:属性
- python 类属性
- python 类属性
- Python类的属性
- python类私有属性
- python 类属性
- Python类属性详解
- 类属性(1)-Python
- python内置类属性
- python-类的属性
- python 类属性
- python类的属性
- canvas绘制动态时钟
- 重磅 | 腾讯提出共享AI,抢先布局游戏与多媒体AI,首秀同声传译
- 【程序员周边】程序员系列表情包
- 11.9第7节课
- Javascript的学习心得
- Python 3.6 类属性
- 11月7日云栖精选夜读:阿里巴巴风鸣:做技术Leader要有危机意识
- 【大数据】R语言速成与实战
- C/C++几个内存处理函数
- Java几种消息对话框
- PCL1.8.1+Qt5.9.1+VS2015联合编译和显示三维图像
- android和js相互调用
- 严重: StandardWrapper.Throwable
- 树莓派中测试USB摄像头