【发布时间】: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