# -*- coding: utf-8 -*-
"""
Created on Sun May 12 22:53:04 2019
@author: User
"""
class Human(object):
domain = 'eukarya'
def __init__(self, kingdom='Animalia',phylum='Chordata',
order='Primates',family='Human Being'):
self.kingdom = kingdom
self.phylum=phylum
self.order = order
self.family = family
def typewrite(self):
print('This is %s typing words!' %self.family)
def add(self, n1, n2):
n = n1 + n2
print(str(n1) + '+' + str(n2) + '+' + str(n))
print('You see! %s can calculate!' %self.family)
@classmethod
def showclassmethodinfo(cls):
print(cls.__name__) #__name__属性,其值为类名
print(dir(cls)) #使用dir列示本类的所有方法
ZhangSan = Human()
print(Human.domain)
print(ZhangSan.domain)
# kingdom是实例属性,只能通过实例名来访问。所以下面这句会出错
print(Human.kingdom)
运行:
eukarya
eukarya
Traceback (most recent call last):
File "<ipython-input-37-b3a1b8a92764>", line 1, in <module>
runfile('D:/0python/2.1.3.1实例属性和类属性_0512.py', wdir='D:/0python')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "D:/0python/2.1.3.1实例属性和类属性_0512.py", line 37, in <module>
print(Human.kingdom)
AttributeError: type object 'Human' has no attribute 'kingdom'