Python_Class
1.创建类的关键字:class
2.构造函数__init__(),自动初始化属性值
class Cup: #构造函数,初始化属性值 def __init__(self,capacity,color): self.capacity=capacity self.color=color def retain_water(self): print("杯子颜色:"+self.color+",杯子容量:"+self.capacity+",正在装水.") def keep_warm(self): print("杯子颜色:"+self.color+",杯子容量:"+self.capacity+",正在保温.") cup1=Cup('450ml','红色') cup2=Cup('700ml','黄色') cup1.retain_water() cup2.keep_warm()