zoukankan      html  css  js  c++  java
  • 私有属性和方法

    # 私有属性:属性名前面有两个下划线,那么这样的属性称为私有属性
    # 私有方法:方法名前面有两个下划线,那么这样的方法称为私有方法

    # 私有属性和私有方法的特点:
    # 私有属性和私有方法只能在当前类中使用,不能类外或者其他类(子类)里面使用


    class Person(object):
    def __init__(self,name,age):
    self.name = name
    self.__age = age

    def show(self):
    print(self.name,self.__age)

    def __myage(self):
    print(self.__age)

    s1 = Person('wj',24)
    print(s1.name)
    # print(s1.__age)
    s1.show()
    # s1.__myage()

    #查看对象私有属性包装后的名字,查看对象的属性
    res = s1.__dict__ #对象名.__dict__
    print(res)
    # 查看类的方法和属性,可以知道私有方法和私有属性包装后的名称
    res = Person.__dict__ #类名.__dict_
    print(res)

    知道后就可以直接点来访问()
    s1._Person__myage()
    print(s1._Person__age)
  • 相关阅读:
    web.xml
    web.xml hello1代码分析
    annotation
    injection
    container
    build tool
    version control
    url与uri的区别
    函数式语言
    http协议解析过程
  • 原文地址:https://www.cnblogs.com/wjun0/p/11515398.html
Copyright © 2011-2022 走看看