gpt4 book ai didi

java - 使用多个类的不兼容返回类型

转载 作者:行者123 更新时间:2023-12-02 10:07:11 30 4
gpt4 key购买 nike

我正在编写一个程序来模拟简单的进程控制 block 队列,并且在 returnPcb() 方法中遇到返回值问题。我收到“无效的返回类型”。我知道我的方法中的返回类型是 Pcb,但我无法更改它。如果对removePcb()的调用为假,我想返回-1的值。我的想法是创建一个新的 Pcb,将值设置为 -1,然后返回该值。这就是我遇到问题的地方。当条件为 false 时,我需要返回 -1 的帮助。谢谢。

MasterQueue 类:

import java.util.*;

public class MasterQueue {

HashMap<String,Queue<Pcb>>hash;

MasterQueue(){
hash = new HashMap<>();
}

public boolean addQueue(String nameIn){
String QueueName = nameIn;
if(hash.containsKey(QueueName)){
return false;
}
//else add new queue the hashmap
else{
Queue<Pcb> q = new LinkedList<>();
hash.put(QueueName,q);
return true;
}
}

public boolean addPcb(Pcb p,String nameIn){
String PcbName = nameIn;
//if queue exist in the list then add the pcb to it
if(hash.containsKey(PcbName)){
hash.get(PcbName).add(p);
return true;
}
//else return false
else{
return false;
}
}

public Pcb removePcb(String nameIn){
String RemovePcbName = nameIn;
//if this queue exist in the list then remove first element from the queue
if(hash.containsKey(RemovePcbName)){
return hash.get(RemovePcbName).remove();
}
Pcb p = new Pcb(0, 0, 0, -1);
return p.getPid();
}

}

PCB 类别:

public class Pcb {


private int low;
private int high;
private int state;
int pid;

Pcb(int lowMemIn, int highMemIn, int stateIn, int pidIn){
setLowMem(lowMemIn);
setHighMem(highMemIn);
setState(stateIn);
setPid(pidIn);
}

public void setLowMem(int lowMemIn){
low = lowMemIn;
}

public int getLowMem(){
return low;
}

public void setHighMem(int highMemIn) {
high = highMemIn;
}

public int getHighMem(){
return high;
}

public void setState(int stateIn){
state = stateIn;
}

public int getState() {
return state;
}

public void setPid(int pidIn){
pid = pidIn;
}

public int getPid(){
return pid;
}
}

测试

@Test
public void testAddPcb1() {
Pcb pid1 = new Pcb(1, 2, 3, 4);
MasterQueue mq1 = new MasterQueue();
mq1.addQueue("miniQueueStr");

Assert.assertTrue("error", mq1.addPcb(pid1, "miniQueueStr"));

最佳答案

您的方法当前定义为:

public Pcb removePcb(String nameIn){
String RemovePcbName = nameIn;
//if this queue exist in the list then remove first element from the queue
if(hash.containsKey(RemovePcbName)){
return hash.get(RemovePcbName).remove();
}
Pcb p = new Pcb(0, 0, 0, -1);
return p.getPid();
}

所以你向编译器 promise 这段代码将返回一个 Pcb 对象,而不是其他任何东西。您不能只是决定让它返回其他内容,它必须是一个 Pcb 对象。

所以这样做:您定义了故障情况 Pcb p = Pcb(0,0,0,-1),因此只需在函数末尾返回该 p编译器会很高兴。

但是,如果没有匹配的 Pcb,您确实不应该返回一个恰好被设置为您认为有意义的值而没有正式将其声明为某个常量的 Pcb 对象,此时事情会变得愚蠢。 .您可能想做的是让您的函数抛出:

public Pcb removePcb(String nameIn) throws NoSuchElementException {
String RemovePcbName = nameIn;
//if this queue exist in the list then remove first element from the queue
if(hash.containsKey(RemovePcbName)){
return hash.get(RemovePcbName).remove();
}
throw new NoSuchElementException();
}

然后在您使用的代码中,将该删除调用放入 try/catch 中,并执行您作为程序员知道的当有人尝试删除不存在的 Pcb 时需要发生的操作。

关于java - 使用多个类的不兼容返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55270700/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com