# -*- coding: utf-8 -*-
"""
Created on Mon May 13 15:50:45 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列示本类的所有方法
class Male(Human):
def __init__(self,gender='male'):
super(Male,self).__init__() #
self.gender=gender
def less(self, n1, n2):
'''为子类增加一个名为less的方法'''
n = n1 - n2
print(str(n1) + '-' + str(n2) + '=' + str(n))
print('你看,%s 可以计算!' %self.gender)
def add(self, n1, n2):
'''为子类重新定义add方法'''
n = n1 - n2
print(str(n1) + '-' + str(n2) + '=' + str(n))
print('你看,%s 不能使用加法计算!' %self.gender)
def showclassmethodinfo(self):
print("这是 Male 的class method 信息:")
super(Male,self).showclassmethodinfo()
print("\n这是 Male 的 instance method 信息:")
print(dir(self))
class Female(Human):
def __init__(self,gender='female'):
super(Female,self).__init__() #
self.gender=gender
def add(self, n1, n2):
'''为子类重新定义add方法'''
n = n1 - n2
print(str(n1) + '-' + str(n2) + '=' + str(n))
print('你看,%s 不能使用加法计算!' %self.gender)
def div(self, n1, n2):
n = n1 / n2
print(str(n1) + '/' + str(n2) + '=' + str(n))
print('你看,%s 能使用除法计算!' %self.gender)
someoneismale = Male()
WangWu = Human()
print(isinstance(someoneismale, Male))
print(isinstance(someoneismale, Human))
#父类的实例与子类不是同一个类型
print(isinstance(WangWu, Male))
print('\n2----------------------------\n')
#定义如下函数调用对象的add方法
def additon(obj, n1, n2):
obj.add(n1, n2)
someoneisfemale = Female()
print(additon(someoneismale, 1, 1))
print(additon(someoneisfemale, 1, 1))
print(additon(WangWu, 1, 1))
print('\n3----------------------------\n')
print(additon(Male(), 1, 1))
print(additon(Female(), 1, 1))
print(additon(Human(), 1, 1))
运行:
True
True
False
2----------------------------
1-1=0
你看,male 不能使用加法计算!
None
1-1=0
你看,female 不能使用加法计算!
None
1+1+2
You see! Human Being can calculate!
None
3----------------------------
1-1=0
你看,male 不能使用加法计算!
None
1-1=0
你看,female 不能使用加法计算!
None
1+1+2
You see! Human Being can calculate!
None