标签:-- span while 多个实例 1.4 调用 update 执行 访问
1 #创建Dog类 2 3 class Dog(): 4 """一次模拟小狗的简单尝试""" 5 6 def __init__(self,name,age): 7 """初始化属性name和age""" 8 self.name = name #像这样通过实例访问的变量称为属性 9 self.age = age 10 11 def sit(self): 12 """模拟小狗被命令时蹲下""" 13 print(self.name.title() + "is now sitting.") 14 15 def roll_over(self): 16 """模拟小狗被命令时打滚""" 17 print(self.name.title() + "rolled over!")
1 class Dog(): 2 """一次模拟小狗的简单尝试""" 3 4 def __init__(self,name,age): 5 """初始化属性name和age""" 6 self.name = name #像这样通过实例访问的变量称为属性 7 self.age = age 8 9 def sit(self): 10 """模拟小狗被命令时蹲下""" 11 print(self.name.title() + "is now sitting.") 12 13 def roll_over(self): 14 """模拟小狗被命令时打滚""" 15 print(self.name.title() + "rolled over!") 16 17 #根据类创建实例 18 my_dog = Dog(‘while‘,6) 19 print("my dog‘s name is " + my_dog.name.title() + ‘.‘) 20 print("my dog is " + str(my_dog.age) + " years old.")
执行结果:
1 my dog‘s name is While. 2 my dog is 6 years old.
1 #访问属性 2 # my_dog.name
根据Dog类创建实例后,就可以使用句点表示法来调用Dog类中定义的任何方法。
1 #调用方法 2 class Dog(): 3 --snip-- 4 my_dog = Dog(‘while‘,6) 5 my_dog.sit() 6 my_dog.roll_over()
要调用方法,可指定实例的名称(这里指my_dog)和调用的方法。
1 #创建多个实例 2 my_dog = Dog(‘while‘,6) 3 your_dog = Dog(‘Harry‘,7) 4 print("My dog‘s is " + my_dog.name.title() + ‘.‘) 5 print("My dog is" + str(my_dog.age) + "years old.") 6 my_dog.sit() 7 8 print("\nYour dog‘s name is " + your_dog.name.title()+‘.‘) 9 print("Your dog is" + str(your_dog.age) + "years old.") 10 your_dog.sit()
执行结果:
1 My dog‘s is While. 2 My dog is6years old. 3 While is now sitting. 4 5 Your dog‘s name is Harry. 6 Your dog is7years old. 7 Harry is now sitting.
#给属性指定默认值
class Dog():
"""一次模拟小狗的简单尝试"""
def __init__(self,name):
"""初始化属性name和age"""
self.name = name #像这样通过实例访问的变量称为属性
self.age = 7 #给属性指定默认值
def get_name(self):
print("My dog‘s name is " + self.name + ‘.‘)
def get_age(self):
"""打印出小狗的年纪"""
print("My dog‘s age is " + str(self.age) + ‘ years old.‘)
my_dog = Dog(‘Harry‘)
my_dog.get_name()
my_dog.get_age()
执行结果:
1 My dog‘s name is Harry. 2 My dog‘s age is 7 years old.
修改属性的值有三种方式:
1 class Dog(): 2 """一次模拟小狗的简单尝试""" 3 4 def __init__(self,name): 5 """初始化属性name和age""" 6 self.name = name #像这样通过实例访问的变量称为属性 7 self.age = 7 #给属性指定默认值 8 9 def get_name(self): 10 print("My dog‘s name is " + self.name + ‘.‘) 11 12 def get_age(self): 13 """打印出小狗的年纪""" 14 print("My dog‘s age is " + str(self.age) + ‘ years old.‘) 15 16 17 my_dog = Dog(‘Harry‘) 18 my_dog.get_name() 19 #直接修改属性的值 20 my_dog.age = 10 21 my_dog.get_age()
执行结果:
1 My dog‘s name is Harry. 2 My dog‘s age is 10 years old.
class Dog(): """一次模拟小狗的简单尝试""" def __init__(self,name): """初始化属性name和age""" self.name = name #像这样通过实例访问的变量称为属性 self.age = 7 #给属性指定默认值 def update_name(self,new_age): self.age = new_age def get_name(self): print("My dog‘s name is " + self.name + ‘.‘) def get_age(self): """打印出小狗的年纪""" print("My dog‘s age is " + str(self.age) + ‘ years old.‘) my_dog = Dog(‘Harry‘) my_dog.get_name() # #直接修改属性的值 # my_dog.age = 10 # my_dog.get_age() #通过方法修改属性的值 my_dog.update_name(25555) my_dog.get_age()
执行结果:
1 My dog‘s name is Harry. 2 My dog‘s age is 25555 years old.
1 class Dog(): 2 """一次模拟小狗的简单尝试""" 3 4 def __init__(self,name): 5 """初始化属性name和age""" 6 self.name = name #像这样通过实例访问的变量称为属性 7 self.age = 7 #给属性指定默认值 8 9 # def update_name(self,new_age):#通过方法修改属性值 10 # self.age = new_age 11 12 def increase_age(self,new_age):#通过方法对属性的值进行递增 13 self.age += new_age 14 15 def get_name(self): 16 print("My dog‘s name is " + self.name + ‘.‘) 17 18 def get_age(self): 19 """打印出小狗的年纪""" 20 print("My dog‘s age is " + str(self.age) + ‘ years old.‘) 21 22 23 my_dog = Dog(‘Harry‘) 24 my_dog.get_name() 25 # #直接修改属性的值 26 # my_dog.age = 10 27 # my_dog.get_age() 28 29 # #通过方法修改属性的值 30 # my_dog.update_name(25555) 31 # my_dog.get_age() 32 33 my_dog.increase_age(30) 34 my_dog.get_age()
执行结果:
1 My dog‘s name is Harry. 2 My dog‘s age is 37 years old.
标签:-- span while 多个实例 1.4 调用 update 执行 访问
原文地址:https://www.cnblogs.com/aszeno/p/10398327.html