您的位置:首页 > 编程语言 > Python开发

Python的类

2015-07-13 20:01 726 查看

Python的类

# coding=utf-8
__author__ = 'zyt'

class Fruit():
name = None
age = 0

def __init__(self):  # 自动被调用
self.name = 'apple'
self.age = 1
print '类名是', self.__class__.__name__

def set_name(self, nm):
self.name = nm

def show_name(self):
print 'name is:', self.name

if __name__ == '__main__':
apple = Fruit()
apple.show_name()
apple.set_name('Red Fuji Apple')
apple.show_name()
print type(apple)
print isinstance(apple, Fruit)

运行结果:
类名是 Fruit
name is: apple
name is: Red Fuji Apple
<type 'instance'>
True
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: