不兼容的对象类型 - Java

时间:2016-03-23 00:47:17

标签: java compiler-errors compiler-warnings

我正在尝试实现一个小型Java程序,该程序显示了进程调度的工作原理。我目前的代码如下。我遇到的问题是来自CPU.executeInstructions(Process process)类的静态方法firstComeFirstServed()ProcessScheduler。我的程序目前不会编译,因为它给出了

incompatible type error: Object can not be converted to Process

来自firstComeFirstServed()

我设法通过将executeInstructions()参数从Process process更改为Object process来进行编译,但据我所知,以前的方法签名没有任何问题。我在程序周围抛出了几个System.out.println()个调用来将类打印到屏幕上,这确认了正在操作的对象是Process个对象。有人能解释一下这里发生了什么吗?我错过了什么/不理解?

package processes;

import java.util.logging.Level;
import java.util.logging.Logger;
import processes.ProcessScheduler.Algorithm;

public class ProcessManager {
    private static Thread psThread;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Create process table
        ProcessTable pt;
        //System.out.println("1 " + pt.getClass());
        // Creat Process Scheduling Thread.
        psThread = new Thread(new ProcessScheduler(Algorithm.FIRST_COME_FIRST_SERVE, pt = new ProcessTable()));
        System.out.println("2 " + pt.getClass());
        // Start Thread
        psThread.start();
        System.out.println("3 " + pt.getClass());
        try {
            // Add Process' to table
            String[] instrucSet = {"sout","name"};
            for(int i = 0; i < 5; i++){
                pt.add(new Process(ProcessTable.processCounter, i + 1, 10 - i, instrucSet));
            }
            Thread.sleep(4000);
            for(int i = 0; i < 5; i++){
                pt.add(new Process(ProcessTable.processCounter, i + 1, 10 - i, instrucSet));
            }
            Thread.sleep(2000);
            ProcessScheduler.run = false;

        } catch (InterruptedException ex) {
            Logger.getLogger(ProcessManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}



package processes;

public class Process {
    private int quanta, priority, pID;
    private String [] instructions;


    /**
     * Constructor for Process class.
     * @param p_id process id
     * @param instruction_set Instructions to be processed by the CPU.
     * @param quanta Represents length of time (known or estimated) taken to execute process
     */
    public Process(int p_id, int quanta, String instruction_set[]){
        // Initialise instance variables
        this.pID = p_id;
        this.quanta = quanta;
        this.instructions = instruction_set;
    }

    /**
     * Constructor for Process class.
     * @param quanta Represents length of time (known or estimated) taken to execute process
     * @param priority Represents the priority of the process, from 1 to infinity with 1 having highest priority.
     * @param instruction_set Instructions to be processed by the CPU.
     */
    public Process(int p_id,int quanta, int priority, String instruction_set[]){
        // Initialise instance variables
        this.pID = p_id;
        this.quanta = quanta;
        this.priority = priority;
        this.instructions = instruction_set;
    }

    /**
     * @return Returns length of process, which may either be a known or estimated quantity.
     */
    public int getQuanta() {
        return quanta;
    }

    /**
     * @return Returns process priority level.
     */
    public int getPriority() {
        return priority;
    }

    /**
     * @return Returns process id, a unique value generated when the process is accepted onto the process table.
     */
    public int getpID() {
        return pID;
    }

    /**
     * @return Returns an array holding the instructions to be processed for this process.
     */
    public String[] getInstructions() {
        return instructions;
    }

    @Override
    public String toString(){
        return "Process ID: " + this.getpID() + ". Quanta: " + this.getQuanta() + ". Priority: " + this.getPriority();
    }

}


package processes;

import java.util.ArrayList;

/**
 *
 * @author dave
 */
public class ProcessTable extends ArrayList {

    // P_id counter;
    public static int processCounter = 0;

    public ProcessTable(){
        super();
    }

    /**
     * Adds the specified process to the collection and increments the processCounter
     * @param aProcess The process to be added to the Process Table.
     * @return Returns true if successfully added.
     */
    public boolean add(Process aProcess){
        boolean sucessful = super.add(aProcess);
        if(sucessful)
            processCounter++;
        return sucessful;
    }

    /**
     * Prints the process table to console.
     */
    public void displayProcessTable(){

        for(int i = 0; i < this.size(); i++){
            System.out.println(this.get(i).toString());
        }
    }
}


package processes;

/**
 *
 * @author dave
 */
public final class CPU {

    private CPU(){

    }

    public static void executeInstructions(Process process){
        System.out.println(process.toString());        
    }

}


package processes;

import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author dave
 */
public class ProcessScheduler implements Runnable {    
    public static boolean run;
    // The algorithm to be used.
    private String alg;
    // To hold reference to a Proces Table.
    public static ProcessTable pt;

    // Class constants for scheduling algorithm type. Used in class constructor.
    // Enum for 
    public enum Algorithm {FIRST_COME_FIRST_SERVE, SHORTEST_JOB_FIRST, ROUND_ROBIN, PRIORITY_QUEUE};  

    /**
     * @param scheduling_algorithm Sets the scheduling algorithm to be used when
     * @param process_table A Process Table instant that Process will be added to.
     * passing jobs to the CPU for execution.
     */
    public ProcessScheduler(Algorithm scheduling_algorithm, ProcessTable process_table){
        //System.out.println("4 " + pt.getClass());
        // Create reference Process Table
        //pt = new ProcessTable();
        pt = process_table;
        System.out.println("5 " + pt.getClass());
        // Start scheduling based on algorithm represented by enum in constructor arg.
        switch(scheduling_algorithm){
            case FIRST_COME_FIRST_SERVE:
                alg = "fcfs";
                break;
            case SHORTEST_JOB_FIRST:
                alg = "sjf";
                break;
            case ROUND_ROBIN:
                alg = "rr";
            case PRIORITY_QUEUE:
                alg = "pq";
            default:
                alg = "pq";
                break;
        }
    }

    /**
     * Start Scheduling processes to the CPU
     */
    public void run() {        
        //boolean run = true;
        int sleepTime = 1000;
        //Display algorithm to screen
        try {
            run = true;
            while(run){
                if(!pt.isEmpty()){
                    switch (alg) {
                        case "fcfs":
                            System.out.println("6 " + pt.getClass());
                            firstComeFirstServed();
                            break;
                        case "sjf":
                            shortestJobFirst();
                            break;
                        case "rr":
                            roundRobin();
                            break;
                        case "pq":
                            priorityQueue();
                            break;
                    }
                } else {
                    Thread.sleep(sleepTime);
                }
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(ProcessScheduler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Executes all processes in Process Table on a First Come First Served 
     * basis (the order in which they were added to the collection).
     */
    private void firstComeFirstServed(){
        System.out.println("7 " + pt.getClass());
        for(int i = 0; i < pt.size(); i++){
            CPU.executeInstructions(pt.get(i));
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            Logger.getLogger(ProcessScheduler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private void shortestJobFirst(){
        System.out.println("in SJF");
    }
    private void roundRobin(){
        System.out.println("in RR");
    }
    private void priorityQueue(){
        System.out.println("in PQ");
    }

}

3 个答案:

答案 0 :(得分:2)

问题是扩展ArrayList的ProcessTable类不是通用的。 您可以先使用

进行修改
public class ProcessTable<E> extends ArrayList<E> {

然后,当您实例化它时,您可以指定ProcessTable将保留在

中的类类型

ProcessTable<Process> pt;

这样编译器就会知道哪个类将返回pt.get(i)调用。

如果没有,它会将其视为对象。

编辑: 正如@EJP指出的那样,更具体的问题是你可能希望使用特定的类类型扩展ArrayList,它可以用于

public class ProcessTable extends ArrayList<Process>

答案 1 :(得分:2)

您的ProcessTable定义应如下所示:

public static class ProcessTable extends ArrayList<Process>

答案 2 :(得分:2)

正如其他人所说,Public Class Form1 Const myPixelHeight = 10 Const myPixelWidth = 10 Dim myGraphics As Graphics Dim myPen As Pen Dim TotalImageHeight As Integer Dim Rec As Integer Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load TotalImageHeight = PictureBox1.Height myGraphics = PictureBox1.CreateGraphics PictureBox1.Image = New Bitmap(500, 10000) myPen = New Pen(Brushes.DarkMagenta, myPixelHeight) End Sub Private Sub DrawOneBit(Record As Integer, Sensor As Integer) Dim HDistance As Integer, VDistance As Integer VDistance = Record * myPixelHeight HDistance = Sensor * myPixelWidth * 1.1 myGraphics.DrawLine(myPen, HDistance, VDistance, HDistance + myPixelWidth, VDistance) End Sub 应该延伸ProcessTable。然而,ArrayList<Process>类根本不存在的原因并不充分。一切都是提供一个错误实现的计数器(不应该是静态的)和显示方法。我会删除它,只使用ProcessTable