python笔记:2.1.3.2私有属性和共有属性

# -*- coding: utf-8 -*-
"""
Created on Sun May 12 22:58:07 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__)
    
        
        
ZhangSan = Human()
print(ZhangSan.family)

# 下面这句访问私有属性,会出错
print(ZhangSan.order)

运行:

Human Being
Traceback (most recent call last):

  File "", line 1, in
    runfile('D:/0python/2.1.3.2私有属性和共有属性_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.2私有属性和共有属性_0512.py", line 39, in
    print(ZhangSan.order)

AttributeError: 'Human' object has no attribute 'order'

 

2.1.4.1隐式继承b:未用super函数初始化父类

# -*- 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列示本类的所有方法

        
class Male(Human):
    def __init__(self,gender='male'):
        self.gender=gender

someoneismale = Male()
print(someoneismale.kingdom)


运行:

Human Being
['_Human__order', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'add', 'domain', 'family', 'kingdom', 'phylum', 'showclassmethodinfo', 'typewrite']
Traceback (most recent call last):

  File "", line 1, in
    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 42, in
    print(someoneismale.__order)

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


runfile('D:/0python/2.1.3.4继承b_0512.py', wdir='D:/0python')
Traceback (most recent call last):

  File "", line 1, in
    runfile('D:/0python/2.1.3.4继承b_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继承b_0512.py", line 36, in
    print(someoneismale.kingdom)

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

 

2.1.4.1隐式继承c:用super函数初始化父类

# -*- 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列示本类的所有方法

        
class Male(Human):
    def __init__(self,gender='male'):
        super(Male,self).__init__() #
        self.gender=gender

someoneismale = Male()
print(someoneismale.kingdom)


运行:

Animalia

你可能感兴趣的:(python学习)