本文介绍了如何在类类型列表中存储对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在列表(类类型)中存储类对象时遇到问题。
在下面的代码中,我获得了员工详细信息(名称和等级)所有..而不是被调用的对象。
我正在寻找python替代'Employee [] emp_obj = java的新员工[6]'。
预期o / p:
I am having problem storing class objects in a list(of class type).
In the below code, i get employee details(name and grade) of all.. instead of the called object.
I am looking for python alternative to 'Employee[] emp_obj = new Employee[6]' of java.
Expected o/p :
#(when it_dep.describe is called, it should print as below )
# Dept Name : IT
# Budget : 3000
# Employees : john(3),krishna(4),pawan(5)
#(when sales_dep.describe is called, it should print as below)
# Dept Name : Sales
# Budget : 3500
# Employees : Rahim(6),Jessy(7),Charan(2)
我是什么尝试过:
What I have tried:
class Employee:
def __init__(self,emp_name,emp_grade):
self.emp_name = emp_name
self.emp_grade = emp_grade
def emp_info(self):
return self.emp_name + "(" + str(self.emp_grade) + ")"
class Department:
def __init__(self,dept):
self.department = dept
__budget = 1000
#------------ In java --------------#
#Employee[] emp_obj = new Employee[6]
#counter=0
#-----------------------------------#
empnames = []
def addEmployee(self, emp_details):
if emp_details.emp_grade >= 5:
self.__budget += 1000
else:
self.__budget += 500
employee_name_grade = emp_details.emp_info()
self.empnames.append(employee_name_grade + ",")
# self.empgrades.append(emp_details.emp_grade)
# self.name_and_grade = self.empnames + self.empgrades
#------------------------ Java way of storing emp class objects --------#
#emp_obj[counter] = emp_details
#counter += 1
#-----------------------------------------------------------------------#
def describe(self):
temp = "Dept Name : " + self.department + " \n Budget : " + str(self.__budget) + "\n Employees : "
#-----------------------
for i in self.empnames:
temp += str(i) + ""
print(temp)
emp1 = Employee("john",3)
emp2 = Employee("Krishna",4)
emp3 = Employee("Rahim",6)
emp4 = Employee("Jessy",7)
emp5 = Employee("Charan",2)
emp6 = Employee("Pawan",5)
#-----------------------------------
it_dep = Department("IT")
sales_dep = Department("Sales")
#-----------------------------------
it_dep.addEmployee(emp1)
it_dep.addEmployee(emp2)
it_dep.addEmployee(emp6)
sales_dep.addEmployee(emp3)
sales_dep.addEmployee(emp4)
sales_dep.addEmployee(emp5)
it_dep.describe()
sales_dep.describe()
推荐答案
def __init__(self,dept):
self.department = dept
__budget = 1000
#------------ In java --------------#
#Employee[] emp_obj = new Employee[6]
#counter=0
#-----------------------------------#
empnames = []
您已将__budget和empnames定义为 [ ],因此它们由该类的所有实例共享。您应该在 __ init __
函数中定义它们:
You have defined __budget and empnames as class variables[^], so they are shared by all instances of the class. You should define them inside the __init__
function instead:
def __init__(self, dept):
self.department = dept
self.empnames = []
self. __budget = 1000
class Employee:
def __init__(self,emp_name,emp_grade):
self.emp_name = emp_name
self.emp_grade = emp_grade
def emp_info(self):
return self.emp_name + "(" + str(self.emp_grade) + ")"
class Department:
def __init__(self,dept):
self.department = dept
self.__budget = 1000
self.employees = []
def addEmployee(self, employee):
if employee.emp_grade >= 5:
self.__budget += 1000
else:
self.__budget += 500
self.employees.append(employee)
def describe(self):
print("Dept Name :", self.department)
print("Budget :", self.__budget)
#-----------------------
print("\nEmployees :")
for i in self.employees:
print(" ", i.emp_info())
#-----------------------------------
it_dep = Department("IT")
it_dep.addEmployee(Employee("john",3))
it_dep.addEmployee(Employee("Krishna",4))
it_dep.addEmployee(Employee("Pawan",5))
sales_dep = Department("Sales")
sales_dep.addEmployee(Employee("Rahim",6))
sales_dep.addEmployee(Employee("Jessy",7))
sales_dep.addEmployee(Employee("Charan",2))
it_dep.describe()
print("\n\n")
sales_dep.describe()
这篇关于如何在类类型列表中存储对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!