标签:ini sel init __init__ __name__ ret res int return
class Cat():
""" 类 """
tag = ‘我是家猫‘
def __init__(self, name, age):
self.name = name
# 私有变量
self.__age = age
def set_age(self, age):
self.__age = age
# return self.__age
def show_info(self):
rest = ‘我叫:{0},今年{1}岁‘.format(self.name, self.__age)
print(rest)
return rest
def eat(self):
""" 吃 """
print("猫喜欢吃鱼")
def catch(self):
""" 抓老鼠 """
print("我能抓老鼠")
if __name__ == ‘__main__‘:
# 实例化
cat_black = Cat(‘小黑‘,2)
cat_black.eat()
cat_black.show_info()
print(‘--------‘)
# print(cat_black.name) # 小黑
# print(cat_black.age)
# 私有变量无法访问
# print(cat_black.__age)
cat_black.name = ‘小白‘
cat_black.show_info() #我叫:小白,今年2岁
print(‘--------‘)
# 设置年龄
# cat_black.set_age(7)
# cat_black.show_info() 我叫:小白,今年7岁
print(Cat.tag)
print(cat_black.tag)
print(‘--------‘)
# 判断是否是类的实例
print(isinstance(cat_black, Cat)) #True
标签:ini sel init __init__ __name__ ret res int return
原文地址:https://www.cnblogs.com/ericblog1992/p/11286921.html