99.
class Dog(): # 用class创建Dog类
"""一次模拟小狗的简单尝试"""
def __init__(self , name , age): # 建立一个方法__init__()
"""初始化属性name和age"""
self.name = name # 形参self必不可少
self.age = age # 以self为前缀可以调用类中所有方法
def sit(self): # 创建方法sit
"""模拟小狗被命令时蹲下"""
print(self.name.title() + " is now sitting.")
def roll_over(self): # 创建方法roll_over
"""模拟小狗被命令时打滚"""
print(self.name.title() + " rolled over!")
my_dog = Dog('willie' , 67) # 创建2个实例
your_dog = Dog('lucy' , 44)
print("My dog;s name is " + my_dog.name.title() + ".")
print("My dog is " = str(my_dog.age) + " years old.")
my_dog.sit() # 调用Dog类中方法
print("\nYour dog's name is " + your_dog.name.title() + ".")
print("Yyour dog is " + str(your_dog.age) + " years old.")
my_dog.roll_over()
2019.12.30
最新推荐文章于 2020-01-27 21:32:43 发布