首页 > 解决方案 > 使用多个类的不兼容返回类型

问题描述

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

主队列类:

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"));

标签: java

解决方案


您的方法当前定义为:

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对象,而不是别的。你不能只是决定让它返回其他东西,它必须是一个 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 时需要发生的事情。


推荐阅读