Python学习教程:面向对象学习实力讲解-创新互联
类的实现
class Cat:
"""猫科动物类"""
tag='我是家猫 '
def __init__ (self,name,age=0): #没有默认值必须要传,且写在前面
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()
类的实例判断 :isinstance(cat_black,Cat)

标题名称:Python学习教程:面向对象学习实力讲解-创新互联
转载来于:http://jxruijie.cn/article/djoeoo.html