首页 > 编程语言 > 详细

Python 私有方法

时间:2018-08-26 11:07:19      阅读:159      评论:0      收藏:0      [点我收藏+]
#私有方法的定义与调用

class Cat:
        def __init__(self):
                self.name = ""
                self.age = 0

        def __changeage(self,_age):
                self.age = _age

        def setage(self,_age):
                if _age < 0:
                        #私有方法的调用
                        self.__changeage(10)
                else:
                        self.__changeage(_age)
        def __str__(self):
                return "age is %d ."%(self.age)

tom = Cat()

tom.setage(11)

#tom.__changeage(13)

print(tom)

tom.setage(-10)

print(tom)

 

Python 私有方法

原文:https://www.cnblogs.com/zhanggaofeng/p/9536463.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!