调用类方法而不在Python中初始化类

时间:2017-11-27 22:31:54

标签: java python class methods

我试图将这个Java代码重新编写到Python中。这是Java代码(完美地运行):

q.setParameter("id", "MAJOR");
  

这是Page Class

public class Main {
    private static Pager demandPager;
    private static ArrayList<Process> runningProcesses = new ArrayList<Process>();
    private static ArrayList<Process> finishedProcesses = new ArrayList<Process>();
    private static int numProcesses;
    private static int numRefPerProcess;

    private static int clock;
    private final static int QUANTUM = 3;

    private static File randNum;
    private static Scanner randReader;

    public static void main(String[] args) {
        readInput(args);
        initializeRandNums();

        int prIdx = 0;
        while(runningProcesses.size() > 0) {
            Process p = runningProcesses.get(prIdx);
            for (int ref = 0; ref < QUANTUM; ref++) {
                demandPager.processRef(p, clock, randReader);
                p.setCurrentReference(p.c

alcNextReference(randReader));
                p.incrRefsMade(); 

            clock++;

            if (p.getRefsMade() == numRefPerProcess) {
                finishedProcesses.add(p);
                runningProcesses.remove(p);
                prIdx--;
                break;
            }
        }

        if (prIdx == numProcesses-1) {
            prIdx = 0;
        }
        else {
            prIdx++;
        }

    }
    printOutput(args);
}

public static void readInput(String[] args) {
    if (args.length != 6) {
        System.out.println("Please input the machine size, page size, process size, job mix, number of references per process, and the replacement algorithm.");
        System.exit(0);
    }

    demandPager = new Pager(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
    Page.setSize(Integer.parseInt(args[1]));
    Process.setSize(Integer.parseInt(args[2]));
    initializeProcesses(args[3]);
    numRefPerProcess = Integer.parseInt(args[4]);
    demandPager.setReplacementAlgo(args[5]);
}

我将其转换为Python的尝试如下:

    import java.util.ArrayList;

public class Page {
    private static int size;
    private int timeEvicted;
    private int timeLoaded;
    private int timeLastUsed;

    private ArrayList<Integer> referencedWords;



private int frameId;
    private int pageId;
    private Process ownerProcess;
    private boolean ifLoaded; 

    public Page(int frame) {
        frameId = frame;
        pageId = -1;
        timeEvicted = 0;
    timeLoaded = 0;
    ifLoaded = false;
    ownerProcess = null;
    referencedWords = new ArrayList<Integer>();
}

public void initalize(Process owner, int pgId, int refdWord, boolean ifLoad) {
    ownerProcess = owner;
    pageId = pgId;
    addRefdWord(refdWord);
    ifLoaded = ifLoad;
}

public static void setSize(int s) {
    size = s;
}

public void setPageId(int p) {
    pageId = p;
}
  

班级页面:

from Process import Process
from Page import Page as Page
from Pager import Pager
from FrameTable import FrameTable 
import sys
#store random numbers in a list
r = open('random.txt')
l = []
for c in r:
    l.append(int(c))
runningProcesses = []
finishedProcesses = []
def main():
    #read input
    machineSize = int(sys.argv[1])
    pageSize = int(sys.argv[2])
    processSize = int(sys.argv[3])
    jobMix = int(sys.argv[4])
    numOfRefPerProcess = int(sys.argv[5])
    replacementAlgo = (sys.argv[6])
    demandPager = Pager(machineSize, pageSize)
    Page.setSize(pageSize)
    Process.setSize(processSize)
    setProc(jobMix)
    demandPager.setAlg(replacementAlgo)

    index = 0
    while(runningProcesses.size() > 0):
        p = runningProcesses.get(index)
        for i in range(3):
            demandPager.processRef(p, clock, randReader)
            p.setCurrentReference(p.calcNextReference(randReader))
            p.incrRefsMade()

            clock+=1

            if (p.getRefsMade() == numRefPerProcess):
                finishedProcesses.add(p)
                runningProcesses.remove(p)
                index-=1
                break
        if (index == numProcesses-1):
            index = 0
        else:
            index+=1
    print "print ng....."
    printOutput(args)

我的问题是,每次我尝试运行此代码时,都会收到此错误: TypeError:必须使用Process实例作为第一个参数调用unbound方法setSize()(改为使用int实例)

我不确定为什么会这样。这在Python中是不合法的还是我错过了什么?

0 个答案:

没有答案