__init__、__str__方法

1.def__init__()方法:

class Cat:
"""定义了一个Cat类"""

#初始化对象
def __init__(self, new_name, new_age):
self.name = new_name
self.age = new_age

#方法
def eat(self):
print("猫在吃鱼....")

def drink(self):
print("猫正在喝kele.....")

def introduce(self):
print("%s的年龄是:%d"%(self.name, self.age))

#创建一个对象
tom = Cat("汤姆", 40)
tom.eat()
tom.drink()
#tom.name = "汤姆"
#tom.age = 40
tom.introduce()

lanmao = Cat("蓝猫", 10)
#lanmao.name = "蓝猫"
#lanmao.age = 10
lanmao.introduce()

2.def__str__()方法:
str是显示给用户用的
class Cat:
"""定义了一个Cat类"""

#初始化对象
def __init__(self, new_name, new_age):
self.name = new_name
self.age = new_age

def __str__(self):
return "%s的年龄是:%d"%(self.name, self.age)

#方法
def eat(self):
print("猫在吃鱼....")

def drink(self):
print("猫正在喝kele.....")

def introduce(self):
print("%s的年龄是:%d"%(self.name, self.age))

#创建一个对象
tom = Cat("汤姆", 40)

lanmao = Cat("蓝猫", 10)


print(tom)
print(lanmao)



猜你喜欢

转载自www.cnblogs.com/loser1949/p/9194026.html