Java中的循环队列
我在java中实现了一个循环队列,它将接受对象(Employee)作为条目。现在我有了一个方法来编辑特定对象的姓氏,但不知何故,我无法从Java中的循环队列,java,object,queue,circular-queue,Java,Object,Queue,Circular Queue,我在java中实现了一个循环队列,它将接受对象(Employee)作为条目。现在我有了一个方法来编辑特定对象的姓氏,但不知何故,我无法从CQueue类访问Employee类中找到的姓氏设置器方法,即使我正在导入所有必需的包。代码如下: //PACKAGES IMPORTED package linearstructures; import dataobjects.*; import linearnodes.*; public class CQueue{ Node front, rear
CQueue
类访问Employee
类中找到的姓氏设置器方法,即使我正在导入所有必需的包。代码如下:
//PACKAGES IMPORTED
package linearstructures;
import dataobjects.*;
import linearnodes.*;
public class CQueue{
Node front, rear, temp;
boolean full = false;
String key;
public AnyClass searchKey(String key)
{
temp = rear.next; // first node
do
{
if (temp.obj.getKey().equals(key))
return temp.obj;
temp = temp.next;
} while (temp != rear.next);
return null;
}
public AnyClass editObject(String key){
int choice, newSeqNo;
double newPay;
boolean exit = false;
Employee etemp = new Employee(); //INCLUDED THIS AFTER EDIT
searchKey(key);
if(searchKey(key) != null){
temp.obj.getData();
System.out.println();
System.out.println("------------------------");
System.out.print("Enter new Salary: ");
newPay = sc.nextDouble();
System.out.println("------------------------");
System.out.println();
etemp.setPay(newPay); //INCLUDED THIS AFTER EDIT
else
System.out.println("NO OBJECT WAS FOUND!");
return null;
}
}
员工
类别:
package dataobjects;
public class Employee extends AnyClass
{
public String surname;
public double pay;
public Employee(){}
public Employee(int seqNo, String surname, double pay)
{
super(seqNo);
this.surname = surname;
this.pay = pay;
}
public double getSalary()
{
return pay;
}
public void setPay(double newPay)
{
pay = newPay;
}
public String getData()
{
return super.getData() + ", Surname: " + surname + ", Pay: " + pay;
}
public String getKey()
{
return surname;
}
}
package dataobjects;
public class AnyClass
{
public int seqNo;
public AnyClass(){}
public AnyClass(int seqNo)
{
this.seqNo = seqNo;
}
public int getseqNo()
{
return seqNo;
}
public void setseqNo(int seqNo) {
this.seqNo = seqNo;
}
public String getData()
{
return "Sequential Number - " + seqNo;
}
public String getKey()
{
return Integer.toString(seqNo);
}
public void edit(){}
}
AnyClass
class:
package dataobjects;
public class Employee extends AnyClass
{
public String surname;
public double pay;
public Employee(){}
public Employee(int seqNo, String surname, double pay)
{
super(seqNo);
this.surname = surname;
this.pay = pay;
}
public double getSalary()
{
return pay;
}
public void setPay(double newPay)
{
pay = newPay;
}
public String getData()
{
return super.getData() + ", Surname: " + surname + ", Pay: " + pay;
}
public String getKey()
{
return surname;
}
}
package dataobjects;
public class AnyClass
{
public int seqNo;
public AnyClass(){}
public AnyClass(int seqNo)
{
this.seqNo = seqNo;
}
public int getseqNo()
{
return seqNo;
}
public void setseqNo(int seqNo) {
this.seqNo = seqNo;
}
public String getData()
{
return "Sequential Number - " + seqNo;
}
public String getKey()
{
return Integer.toString(seqNo);
}
public void edit(){}
}
节点
类
对于include pay“etemp.setPay(newPay);”,您将把返回对象更改为Employee
public Employee editObject(String key){
Employee etemp = new Employee(); //INCLUDED THIS AFTER EDIT
....
....
etemp.setPay(newPay); //INCLUDED THIS AFTER EDIT
return etemp;
}
因为AnyClass没有“公共双倍支付;”您的
editobject
方法可能是这样的:
public AnyClass editObject(String key){
// ...
// Store your search result to avoid performing searchKey twice
AnyClass searchResult = searchKey(key);
if( searchResult != null ){
// Check if searchResult is an Employee object
if( searchResult instanceof Employee )
// Cast AnyClass to Employee
Employee empl = (Employee) searchResult;
// Your logic here...
// Set properties of the Employee object
empl.setPay(newPay);
// ...
return empl;
}
// Add 'else if' here, if you need to manage other Object types
// otherwise you can join previous if conditions
}
else
System.out.println("NO OBJECT WAS FOUND!");
return null;
}
您的代码创建了一个新的本地
Employee
实例,该实例在方法结束时死亡,其值将丢失,因为没有对象指向它。什么是CQueue?这个的代码在哪里?@dbrown93它的代码是第一段代码。我只包括了相关的方法(搜索和编辑)和一些声明。它是实现循环队列的类。您所说的“问题”是什么意思?是否存在编译器错误或引发异常?@dbrown93表示setPay()
方法不存在。问题是它正在查看AnyClass
但是setPay()
位于Employee
类中。我的问题是如何访问该类。您通过employee对象访问setPay方法,如果临时节点中的“obj”不是employee,编译器将不知道该方法在哪里