python笔记:2.1.2.1实例方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bq_cui/article/details/90144616
# -*- coding: utf-8 -*-
"""
Created on Sun May 12 15:17:57 2019

@author: User
"""

class Human(object):
    kingdom = 'Animalia'
    phylum = 'Chordata'
    order = 'Primates'
    family = 'Human Bing'
    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)
        
ZhangSan = Human()
print(ZhangSan.typewrite())
print(ZhangSan.add(1, 1))   

#重新声明 human类,具有方法
print('\n2------------------\n')
class Human2(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)
        
        
ZhangSan2 = Human2()
LiSi = Human2(kingdom='Animal', family = 'Homo')
print(LiSi.kingdom)
print(LiSi.family)
print(ZhangSan.kingdom)

运行:

This is Human Bing typing words!
None
1+1+2
You see! Human Bing can calculate!
None

2------------------

Animal
Homo
Animalia

猜你喜欢

转载自blog.csdn.net/bq_cui/article/details/90144616