python 面向对象 (一)

class people:
    #定义基本属性
    name = ''
    age = 0
    #定义私有属性,私有属性在类外部无法直接进行访问
    __weight = 0
#    定义构造方法
    def __init__(self,n,a,w):
        self.name = n
        self.age = a
        self.__weight = w
    def speak(self):
        print("%s 说: 我 %d 岁。" %(self.name,self.age))
    def out_weight(self):
        print('my weight is ',self.__weight)
        
        return self.__weight
p = people('lg',30,70)
p.speak()

print(p.name,p.age)

p.out_weight()
print(p.__weight)
lg 说: 我 30 岁。
lg 30
my weight is  70
AttributeError: 'people' object has no attribute '__weight'
posted @   luoganttcc  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示