'''创建类和使用类'''
class Dog:
'''初始化属性'''
def __init__(self,name,age):
self.name=name
self.age=age
'''定义方法'''
def sit(self):
print(f"The {self.name} is sitting\n")
def run(self):
print(f"The {self.name} is running\n")
'''传递实参'''
my_dog=Dog("馒头",2)
'''访问属性(my_dog.name)'''
print(f"My dog's name is {my_dog.name}, and it's age is {my_dog.age}\n ")
'''调用方法'''
my_dog.sit()
my_dog.run()
print("-------------------")
'''创建多个实例'''
first_dog=Dog("luck",3)
second_dog=Dog("kitty",5)
print(f"My dog's name is {first_dog.name}, and it's age is {first_dog.age}\n ")
first_dog.sit()
first_dog.run()
print(f"My dog's name is {second_dog.name}, and it's age is {second_dog.age}\n ")
second_dog.sit()
second_dog.run()
python(类)day01
最新推荐文章于 2024-11-18 08:41:09 发布