我正在尝试构建一个可用于实例化新工作项目的类。我们的想法是每个项目都会调用来自不同部门的资源,我需要能够在 Project 对象中实现。每个 Department 应该能够访问项目级属性和方法,以及能够访问各个部门属性的项目对象(即汇总部门成本以返回整体项目成本)。
以下是我正在尝试做的一些伪代码,并希望得到关于正确方法的任何指导:
class Project:
totalCost=0
def addDepartment(Dept):
totalCost+=name.DepartmentCost
Departments=append(Dept)
class Department1:
def __init__(self,Name, NumStaff, AverageStaffSalary)
Name=None
NumStaff=None
AverageStaffSalary=None
DepartmentCost=AverageStaffSalary*NumStaff
Department1_SpecificParm=1
class DepartmentN:
def __init__(self,Name, NumStaff, AverageStaffSalary)
Name=None
NumStaff=None
AverageStaffSalary=None
DepartmentCost=AverageStaffSalary*NumStaff
DepartmentN_SpecificParm="A"
p1=Project("myProject")
p1.addDepartment(Department1(Name="myFirstDepartment", NumStaff=9, AverageStaffSalary=1000))
print p1.myFirstDepartment.DepartmentCost #Print 9000
p1.addDepartment(DepartmentN(Name="myNthDepartment", NumStaff=2, AverageStaffSalary=6000))
print p1.myNthDepartment.DepartmentCost #Print 12000
p1.TotalCost #Print 21000
答案 0 :(得分:0)
(发表评论中提供的答案)
你的代码没有多大意义(即使对于psudo代码),但我认为这可能与你所寻找的一致。 Repl.it playground
class Project:
total = 0
deps = []
def addDep(self, dep):
self.total += dep.cost
self.deps.append(dep)
class Department:
def __init__(self, ppl, salary):
self.cost = ppl * salary
p = Project()
p.addDep(Department(10, 200))
p.addDep(Department(1, 3000))
print(p.total)