题目描述
代码实现1
fo = open("PY301-1.txt","w")
class Horse():
def __init__(self, category, gender, age):
self.category=category
self.gender=gender
self.age=age
self.horse_speed=0
def get_descriptive(self):
info = 'this horse is ' +str(self.age)+ ' years old, ' + 'its category is ' + self.category+ ',' + ' its fender is ' + self.gender + '.'
fo.write(info+"\n")
def update_speed(self, new_speed):
self.horse_speed=new_speed
fo.write('the speed of this horse is ' + str(self.horse_speed)+"km/h.")
hourse=Horse("Arab","male","12")
hourse.get_descriptive()
hourse.update_speed(50)
fo.close()
代码实现2
fo = open("PY301-2.txt", "w")
class Horse():
def __init__(self, category, gender, age):
self.category = category
self.gender = gender
self.age = age
self.horse_speed = 0
def get_descriptive(self):
info = 'this horse is ' + str(
self.age) + ' years old, ' + 'its category is ' + self.category + ',' + ' its fender is ' + self.gender + '.'
fo.write(info + "\n")
def update_speed(self, new_speed):
self.horse_speed = new_speed
fo.write('the speed of this horse is ' + str(self.horse_speed) + "km/h.\n")
class Camel(Horse):
def __init__(self, category, gender, age):
super().__init__(category, gender, age)
self.hump_size = 2
def describe_hump_size(self):
fo.write('this camel has ' + str(self.hump_size) + ' hump.')
camel = Camel("Double hump", "female", "20")
camel.get_descriptive()
camel.update_speed(40)
camel.describe_hump_size()
fo.close()