class the Person:
DEF __init__ (Self, new_name, weight):
# . Self attribute = parameter
self.name = new_name
self.weight = weight
DEF __str__ (Self):
return " My name is% s,% .2f weight is kg " % (self.name, self.weight)
DEF eAT (Self):
Print ( " % S is a food goods, eating and then lose weight " % self.name)
self.weight + = 1 DEF RUN (Self):
Print ( " % S love jogging, jogging exercise " %
self.name)
self.weight-=0.5
person1=Person("小明",75.0)
person2=Person("小美",45.0)
person1.run()
person1.eat()
print(person1)
person2.eat()
person2.run()
print(person2)
The use of the class, you should develop.
class HouseItem:
def __init__(self,name,area):
self.name=name
self.area=area
def __str__(self):
return "[%s]占地%.2f" %(self.name,self.area)
#类与类之间两个空行
class House:
def __init__(self, house_type, area):
self.house_type = house_type
self.area = area
self.free_area=area
self.item_list=[]
DEF __str__ (Self):
return ( " Unit is% s \ n is the total area% .2f \ n is the remaining area% .2f \ n furniture names are S% "
% (self.house_type,
self.area,
self.free_area ,
self.item_list))
DEF add_item (Self, Item):
Print ( " you want to add% s " % Item)
IF item.area> self.free_area:
Print ( " area% s not too much to add. " % Item. name)
return
self.item_list.append (item.name)
self.free_area- = item.area
# create furniture
Bed = HouseItem ( " Simmons " , 40 )
Chest = HouseItem ( " wardrobe " , 20 )
the Table = HouseItem ( " table " , 1.5 )
Print (Bed)
Print (Chest)
Print (the Table)
# create a house objects
my_home = house ( " Bedroom " , 60 )
my_home.add_item (Bed)
my_home.add_item (Chest)
my_home.add_item (the Table)
Print (my_home)