【问题标题】:Import closed methods and attributes导入封闭的方法和属性
【发布时间】:2017-06-29 09:55:23
【问题描述】:

我正在尝试导入我的类,以便在另一个类中使用它的属性。 是否可以为派生类调用基本类的方法?在基本类中有方法,它们有封闭的方法,里面有封闭的属性。

或者我应该打开它的方法和属性来调用它们吗? 所以,我想给你一个例子,请帮助我?我有点迷路了。

我有一个基础课(1):

   class Car (object):
    """Virtual car"""

   total=0

   def __init__(self,petrol=100): 
        self.__petrol=petrol

    @property
        def petrol(self):
        return self.__petrol

    @petrol.setter
    def petrol(self,new_petrol):
        self.__petrol=new_petrol
       print("The level of petrol was changed")

    def __str__(self):
         print("Car condition")
         print(" "+ self.__petrol )

    def __addPetrol(self):
         print("Welcome to petrol station!")
         addPetr=int(input("Enter quanity of litres: "))
         self.__petrol=self.petrol+addPetr
         print("Thank you for visit the OIL service!")

    def __visitSrvice(self):
       print("""
           1. pour some petrol
           2. poor some oil
              """)
       visit=int(input("Choose a turn"))
       if 1==visit :
           self.__addPetrol()

       if 2==visit :
        ..............
    def go(self):
          print("""
          1. Go on your journey
          2. Visit a service
              """)
          gg = int(input("Choose  your action"))
           if 1==gg :
              ..................
          if 2==gg :
               self.__visitSrvice()

      if __name__ == "__main__":
            print("Use modul correctly")
            input("press enter to out")

那我尝试将基础类导入另一个文件,并调用一个函数,请看:

  import Mcar

  class Chassis_Engine(object):
        def Deterioration(self):


        def Recovering(self):
            Mcar.Car.visitService(self)

  ce=Chassis_Engine()
  ce.Recovering()

当然,我有一个错误,上面写着:

Traceback(最近一次调用最后一次): 文件“E:/car/car.py”,第 11 行,在 ce.Recovering()

文件“E:/car/car.py”,第 8 行,在 Recovering Mcar.Car.visitService(self)

AttributeError:类型对象“Car”没有属性“visitService”

请告诉我,如果我不想打开封闭的方法和属性,我可以这样做吗?如果没有,我该怎么走?谢谢。

【问题讨论】:

    标签: python


    【解决方案1】:

    有一个名为dir 的函数为您提供类提供的所有方法。观察:

    >>> class A:
    ...   def __hidden():
    ...     print('hello')
    ...     
    >>> dir(A)
    ['_A__hidden', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
    

    第一种方法 (_A__hidden) 就是您要寻找的方法。

    一般来说,Python 中没有真正的“私有”方法。相反,您可以使用这些“双下划线”方法,不过,这些方法是公开的,但修改了名称_ClassName__MethodName

    【讨论】:

    • 谢谢,我明白了,但如果我尝试这样做:`def Recovering(self): Mcar.Car.go(self)`。然后它调用它,但我有错误,即:AttributeError: 'Chassis_Engine' object has no attribute '_Car__in_action'。你能说,我能用它做什么?
    • @X21,我在您发布的代码中没有看到任何名为__in_action 的方法。此外,您应该在Car外部 使用该技术。您可以在Chassis_Engine.Recovering:Mcar.Car._Car__visitService(self) 中尝试这样做。
    • 我不明白,我确实喜欢这样:Mcar.Car._Car__visitService(self),但它说:AttributeError: 'Chassis_Engine' object has no attribute '_Car__addPetrol'。为什么不能调用?在我看来,当我在派生类中写self 时,它指的是Chassis_Engine,而不是Car
    • @X21,因为您以某种方式将Chassis_Engineself 传递给Mcar.Car.__visitService。我认为,您首先不应该这样做,因为Mcar.Car.__visitService 不接受除隐式self 之外的任何参数。您可能在某处有一些不正确的缩进。
    • 哦,我确实是这样的:Mcar.Car._Car__visitService(Mcar.Car) 并在 Mcar 中更改:self.__addPetrol(self)def __visitService(self): 和这里 self.__in_action(self) 。但是,请最后一次帮助我,如果可以的话,现在我在这行 self.petrol=self.petrol+addPetr 中有一个错误。它说:`self.petrol=self.petrol+addPetr TypeError: unsupported operand type(s) for +: 'property' and 'float'`。为什么:((((((
    猜你喜欢
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 2011-02-23
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    相关资源
    最近更新 更多