如何在pycharm中的不同文件上创建多个类

时间:2019-04-23 13:56:34

标签: python-3.x oop

我有2个文件,我只想在一个文件上创建每个类,但出现错误

Devolper.py文件

from venv.py.com.smart.pk.Employee import Employee
class Devolper(Employee):
    def __init__(self, fname, lname, pay,pro_lang):
        super.__init__(fname, lname, pay)
        self.pro_lang=pro_lang


Employee.py文件

import datetime

class Employee:
   # raise_amount = 1.4
   num_of_emp = 0

   def __init__(self, fname, lname, pay):
       self.fname = fname
       self.lname = lname
       self.pay = int(pay)
       self.email = str(fname).lower()[0] + str(lname).lower() + '@smartsystemeg.com'
       Employee.num_of_emp += 1
       self.raise_amount = 1.4

   def full_name(self):
       return '{} {}'.format(self.fname, self.lname)

   def applay_raise(self):
       return self.pay * self.raise_amount

   def set_amount(self,amt):
       self.raise_amount=amt
   @classmethod
   def newEmployee(cls,emp):
       fname, lname, pay = emp.split('-')
       return cls(fname,lname,pay)

   @staticmethod
   def isWorkday(day):
       if day.weekday()==4 or day.weekday==5:
           return False
       return True

当我在Devolper.py文件中运行此代码时

emp1= Devolper("Hussin","Mahmoud",190,"java")
print(emp1.__dict__)

我遇到错误

/root/PycharmProjects/PythOOPTest/venv/bin/python /root/PycharmProjects/PythOOPTest/venv/com/smart/pk/Devolper.py
Traceback (most recent call last):
  File "/root/PycharmProjects/PythOOPTest/venv/com/smart/pk/Devolper.py", line 2, in <module>
    from venv.py.com.smart.pk.Employee import Employee
ModuleNotFoundError: No module named 'venv.py'

Process finished with exit code 1

但是,如果我将类'Devolper'复制到Employee.py文件中并运行代码,则它运行良好,没有任何错误

1 个答案:

答案 0 :(得分:0)

如果两个文件位于同一文件夹中,请按照以下步骤导入,否则请检查this site

from Employee import Employee    


class Devolper(Employee):
    def __init__(self, fname, lname, pay, pro_lang):
        super().__init__(fname, lname, pay)            # it should be super()
        self.pro_lang=pro_lang


emp1= Devolper("Hussin", "Mahmoud", 190, "java")
print(emp1.__dict__)

输出:

{'fname': 'Hussin', 'lname': 'Mahmoud', 'pay': 190, 'email': 'hmahmoud@smartsystemeg.com', 'raise_amount': 1.4, 'pro_lang': 'java'}