问题描述
所以我在一个主程序中得到了这个。如下所示,我首先尝试通过将单个 Employee 对象提高 10% 来编辑薪酬“45.0”。在这里,我确保我的数学计算是正确的。在另一个类中,我需要编写一个代码,将 10% 添加到所有员工中。
temp.obj.getData()
显示主类中输入的所有员工数据,而新对象 e
为空。有人知道如何将数据从主程序获取到对象 e 或不创建对象 e。我的问题是,当我对对象 e 执行相同的操作时,薪酬的计算都是 0。我认为这是由于 pay = 0
。我似乎在从主类中的对象获取数据(支付值)时遇到问题。有人可以帮忙吗?谢谢如果需要更多代码问我。
主要程序:
etemp[15] = new Employee(134,"Nicole",45.0);
queue.put(etemp[15]);
if(queue!=null){
//Attempt 1 : Editing pay of a single employee
System.out.println("not empty!");
double x = etemp[15].getSalary();
double calc = (x*0.10)+x;
etemp[15].setPay(calc);
System.out.println("Raise: "+etemp[15].getData());
//Attempt 2 : Using a method to edit pay of ALL employee
queue.pay(10);
} else {
System.out.println("empty");
}
其他类中的方法:
public void pay(int p){
int id = 0;
String k = "";
double pay = 0;
Employee e = new Employee(id,k,pay);
CircQueue q = new CircQueue(20);
if(rear!=null){
Node temp=front;
do{
for (int i= 0; i<currNodes; i++)
{
System.out.println(temp.obj.getData());
q.put(e);
q.listAll();
double x = e.getSalary();
double calc = (x*0.10)+x;
System.out.println("Raise: "+calc);
temp = temp.next;
}
}while(!(temp==rear.next));
} else System.out.println("Empty queue!");
}
解决方法
您必须使用 this
方法将您自己的实例作为参数传递。如果我理解正确,您需要在另一个类中使用 etemp
数组,该类具有 pay
方法。如果是这样,您就不必创建更多对象。
etemp[15] = new Employee(134,"Nicole",45.0);
queue.put(etemp[15]);
if(queue!=null){
//Attempt 1 : Editing pay of a single employee
System.out.println("not empty!");
double x = etemp[15].getSalary();
double calc = (x*0.10)+x;
etemp[15].setPay(calc);
System.out.println("Raise: "+etemp[15].getData());
//Attempt 2 : Using a method to edit pay of ALL employee
queue.pay(this,10); //Adding our own instance
} else {
System.out.println("empty");
}
其他类中的方法:
public void pay(MainClass m,int p){
// Here you can use m as the instance,it will have the etemp. Normally,atributes are private,so you have to implement some kind of getters to reach to that array
int id = 0;
String k = "";
double pay = m.getOriginalPay(id); // For example
Employee e = new Employee(id,k,pay);
CircQueue q = new CircQueue(20);
if(rear!=null){
Node temp=front;
do{
for (int i= 0; i<currNodes; i++)
{
System.out.println(temp.obj.getData());
q.put(e);
q.listAll();
double x = e.getSalary();
double calc = (x*0.10)+x;
System.out.println("Raise: "+calc);
temp = temp.next;
}
}while(!(temp==rear.next));
} else System.out.println("Empty queue!");
}
告诉我有没有帮到你。