首页 技术 正文
技术 2022年11月9日
0 收藏 972 点赞 5,001 浏览 1941 个字

类属性

1.类属性

类属性又称为静态变量,或者是静态数据。这些数据是与它们所属的类对象绑定的,不依赖于任何类实例。

2.增 删 改 查

class zoo:
country = 'china'
def __init__(self,name,address,kind):
self.name = name
self.address = address
self.kind = kind
def monkey(self):
print('this is monkey (%s)' %self.address)
def tiger(self):
print('this is tiger (%s)' %self.kind)
def end(self):
print('welcome to %s' %self.name)
dwy = zoo('chinese\'s zoo','beijing','others')zoo.monkey(dwy)
zoo.tiger(dwy)
zoo.end(dwy)# 对类的数据属性增 删 改 查print(zoo.country) # 查看信息zoo.country = 'US' # 修改信息
print(zoo.country)zoo.snake = 'this is snake' # 增加
print(dwy.snake)del zoo.country # 删除
del zoo.snakeprint(zoo.__dict__)

运行结果:

this is monkey (beijing)
this is tiger (others)
welcome to chinese's zoo
china
US
this is snake
{'__module__': '__main__', '__init__': <function zoo.__init__ at 0x7fb30efb8d90>, 'monkey': <function zoo.monkey at 0x7fb30efb8f28>, 'tiger': <function zoo.tiger at 0x7fb30efc9048>, 'end': <function zoo.end at 0x7fb30efc9378>, '__dict__': <attribute '__dict__' of 'zoo' objects>, '__weakref__': <attribute '__weakref__' of 'zoo' objects>, '__doc__': None}Process finished with exit code 0

3.

class zoo:
country = 'china'
def __init__(self,name,address,kind):
self.name = name
self.address = address
self.kind = kind
def monkey(self):
print('this is monkey (%s)' %self.address)
def tiger(self):
print('this is tiger (%s)' %self.kind)
def end(self):
print('welcome to %s' %self.name)
dwy = zoo('chinese\'s zoo','beijing','others')# 调用
zoo.monkey(dwy)
zoo.tiger(dwy)
zoo.end(dwy)# 类的函数属性的增加def eat(self,food):
print('%s 正在吃%s'%(self.name,food))
zoo.eat = eat
print(zoo.__dict__) # 以列表的方式 查看是否添加进去了dwy.eat('草')

运行结果:

this is monkey (beijing)
this is tiger (others)
welcome to chinese's zoo
{'__module__': '__main__', 'country': 'china', '__init__': <function zoo.__init__ at 0x7f90cdc3ed90>, 'monkey': <function zoo.monkey at 0x7f90cdc3ef28>, 'tiger': <function zoo.tiger at 0x7f90cdc4f048>, 'end': <function zoo.end at 0x7f90cdc4f378>, '__dict__': <attribute '__dict__' of 'zoo' objects>, '__weakref__': <attribute '__weakref__' of 'zoo' objects>, '__doc__': None, 'eat': <function eat at 0x7f90cdd49268>}
chinese's zoo 正在吃草Process finished with exit code 0
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849