赞
踩
- class Flower (object) :
- price = 100
- def __init__ (self,c):
- self. color=c
- flower = Flower("Yellow")
- #访问对象和类的数据成员
- print (flower.color,flower.price)
- #修改类的属性
- Flower. price = 80
- #动态增加类的属性
- Flower.name = "郁金香"
- #修改实例的颜色属性为紫色
- flower.color = "purple"
- print (flower.name,flower.color,flower.price)
- def flowerLanguage (self,w):
- self.wish = w
- import types
- #动态为对象增加成员方法
- flower.flowerLanguage = types. MethodType (flowerLanguage, flower)
- #调用对象的成员方法
- flower.flowerLanguage("忠贞的爱")
- print (flower.wish)
class Flower (object) :定义一个叫Flower的类。
price = 100
将price属性赋值为100。
def __init__ (self,c):
self. color=c
flower = Flower("Yellow")实例化一个对象。
print (flower.color,flower.price)
打印color属性和prince属性
Flower. price = 80
将price属性修改为80。
Flower.name = "郁金香"
flower.colo
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。