对类中私有化的理解

class Person(object):
    def __init__(self,name,age,taste):
        self.name = name
        self._age = age
        self.__taste = taste
    def showPerson(self):
        print(self.name)
        print(self._age)
        print(self.__taste)
    def do_work(self):
        self._work()
        self.__away()
    def _work(self):
        print("_work方法被调用")
    def __away(self):
        print("__away方法被调用")
class Student(Person):
    def construction(self,name,age,taste):
        self.name = name
        self._age = age
        self.__taste = taste
    def showStudent(self):
        print(self.name)
        print(self._age)
        print(self.__taste)
    @staticmethod
    def testbug():
        _Bug.showbug()
class _Bug(Student):
    @staticmethod
    def showbug():
        print("showbug函数开始运行")
s1 = Student('Xiaoming',22,'basketball')
s1.showPerson()
# s1.showStudent()
# s1.construction( )
s1.construction('rose',18,'football')
s1.showPerson()
s1.showStudent()
Student.testbug()
'''
Xiaoming
22
basketball
rose
18
basketball
rose
18
football
showbug函数开始运行
'''

2020-05-08

原文地址:https://www.cnblogs.com/hany-postq473111315/p/12846958.html