zoukankan      html  css  js  c++  java
  • python 面向对象 私有属性

    __init__构造函数

    self.name = name # 属性, 实例变量,成员变量,字段

    def sayhi()# 方法, 动态属性

    私有属性不对外看到 前面加上__
    class role(): # 传参数
    
        def __init__(self,name,role,weapon,life_value=100,moneny=15000):
            # 构造函数
            # 实例化过程中做一些类的初始化工作
            self.name = name
            self.role = role
            self.weapon = weapon
            self.life_value = life_value
            self.moneny = moneny
            self.__heart = 'normal'   # 私有属性
    
        def shot(self):  # 类的方法,功能(动态属性)
            print("shotting")
    
        def got_shot(self):
            print("on,%s got shoot..." % self.name)
    
        def buy_gun(self,gun_name):
            print("%s just bought %s" %(self.name,gun_name))
            self.weapon = gun_name
    
    b1 = role('mike','police','AK47')
    
    print(b1.name)
    print(b1.__heart)
    
    # 私有属性不对外公开
    '''
    mike
    Traceback (most recent call last):
      File "C:/Users/Administrator/PycharmProjects/ss/SSS/day6/私有属性1.py", line 32, in <module>
        print(b1.__heart)
    AttributeError: 'role' object has no attribute '__heart'
    '''
    在内部方法使用
    class role(): # 传参数
    
        def __init__(self,name,role,weapon,life_value=100,moneny=15000):
            # 构造函数
            # 实例化过程中做一些类的初始化工作
            self.name = name
            self.role = role
            self.weapon = weapon
            self.life_value = life_value
            self.moneny = moneny
            self.__heart = 'normal'   # 私有属性
    
        def shot(self):  # 类的方法,功能(动态属性)
            print("shotting")
            print("私有属性", self.__heart)
            self.__heart = 'dddd'
    
            print("私有属性", self.__heart)
    
        def got_shot(self):
            print("on,%s got shoot..." % self.name)
    
        def buy_gun(self,gun_name):
            print("%s just bought %s" %(self.name,gun_name))
            self.weapon = gun_name
    
    b1 = role('mike','police','AK47')
    
    print(b1.shot())
    
    
    '''
    shotting
    私有属性 normal
    私有属性 dddd
    None
    '''
    
    

    如果想强制对外能访问

    class role(): # 传参数
    
        def __init__(self,name,role,weapon,life_value=100,moneny=15000):
            # 构造函数
            # 实例化过程中做一些类的初始化工作
            self.name = name
            self.role = role
            self.weapon = weapon
            self.life_value = life_value
            self.moneny = moneny
            self.__heart = 'normal'   # 私有属性
    
        def shot(self):  # 类的方法,功能(动态属性)
            print("shotting")
            print("私有属性", self.__heart)
            self.__heart = 'dddd'
    
            print("私有属性", self.__heart)
    
        # 定义一个方法用来对外访问
        def show_heart(self):
            return self.__heart
    
        def got_shot(self):
            print("on,%s got shoot..." % self.name)
    
        def buy_gun(self,gun_name):
            print("%s just bought %s" %(self.name,gun_name))
            self.weapon = gun_name
    
    b1 = role('mike','police','AK47')
    
    print(b1.show_heart())
    
    # normal

    强制访问私有属性

    class role(): # 传参数
    
        def __init__(self,name,role,weapon,life_value=100,moneny=15000):
            # 构造函数
            # 实例化过程中做一些类的初始化工作
            self.name = name
            self.role = role
            self.weapon = weapon
            self.life_value = life_value
            self.moneny = moneny
            self.__heart = 'normal'   # 私有属性
    
        def shot(self):  # 类的方法,功能(动态属性)
            print("shotting")
    
        def got_shot(self):
            print("on,%s got shoot..." % self.name)
    
        def buy_gun(self,gun_name):
            print("%s just bought %s" %(self.name,gun_name))
            self.weapon = gun_name
    
    b1 = role('mike','police','AK47')
    
    # 强制访问私有属性
    print(b1._role__heart)
    
    b1._role__heart
    
    # normal

     私有属性 继承重构方法不能访问 父类的私有属性

    class Foo(object):
    
    
        def __init__(self, name):
            self.__name = name
    
        def f1(self):
            print(self.__name)
    
    class Bar(Foo):
    
        def f2(self):
            print(self.__name)
    
    obj = Bar("mike")
    obj.f2()
    
    '''
    Traceback (most recent call last):
      File "C:/Users/Administrator.QH-20170325TNQR/PycharmProjects/SSSSS/SS/day7/S13 面向对象/私有属性 继承1.py", line 21, in <module>
        obj.f2()
      File "C:/Users/Administrator.QH-20170325TNQR/PycharmProjects/SSSSS/SS/day7/S13 面向对象/私有属性 继承1.py", line 18, in f2
        print(self.__name)
    AttributeErr
    只有父类自己才能访问

    class Foo(object):
    
    
        def __init__(self, name):
            self.__name = name
    
        def f1(self):
            print(self.__name)
    
    class Bar(Foo):
    
        def f2(self):
            print(self.__name)
    
    obj = Bar("mike")
    obj.f1()
    
    # mike
    
    
    


  • 相关阅读:
    Servant:基于Web的IIS管理工具
    mono-3.4.0 源码安装时出现的问题 [do-install] Error 2 [install-pcl-targets] Error 1 解决方法
    使用 OWIN Self-Host ASP.NET Web API 2
    Xamarin和微软发起.NET基金会
    SQLite vs MySQL vs PostgreSQL:关系型数据库比较
    Mono 3.2.7发布,JIT和GC进一步改进
    如何使用Microsoft技术栈
    c#开源消息队列中间件EQueue 教程
    通过一组RESTful API暴露CQRS系统功能
    NEsper Nuget包
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/8409662.html
Copyright © 2011-2022 走看看