python笔记:2.1.3.4继承

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bq_cui/article/details/90173379
# -*- coding: utf-8 -*-
"""
Created on Sun May 12 23:13:39 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列示本类的所有方法
    @staticmethod
    def showclassmethodinfo():
        print(Human.__name__)
        
class Male(Human):
    pass

someoneismale = Male()
print(someoneismale.family)

#父类私有成员是不能继承的,下面这句出错
print(someoneismale.__order)

运行:

Human Being
Traceback (most recent call last):

  File "<ipython-input-52-5f8db62bf0fb>", line 1, in <module>
    runfile('D:/0python/2.1.3.4继承_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.4继承_0512.py", line 40, in <module>
    print(someoneismale.__order)

AttributeError: 'Male' object has no attribute '__order'

猜你喜欢

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