Python 如何访问另一个类的对象?

Python 如何访问另一个类的对象?,python,class,oop,object,Python,Class,Oop,Object,所以我想实现两个类JobQueue和Process。其思想是JobQueue管理Process类型的对象,并以最短的处理时间对对象进行优先级排序。因此,第一个属性应该表示进程的ID,第二个属性应该是处理时间,两者都是整数。我的类如何从另一个类访问这些对象 >>>class Process: def __init__(self,pid,procTime): self.pid=str(pid) self.procTime=procTime &

所以我想实现两个类
JobQueue
Process
。其思想是
JobQueue
管理
Process
类型的对象,并以最短的处理时间对对象进行优先级排序。因此,第一个属性应该表示进程的ID,第二个属性应该是处理时间,两者都是整数。我的类如何从另一个类访问这些对象

>>>class Process:

     def __init__(self,pid,procTime):
       self.pid=str(pid)
       self.procTime=procTime
>>>p1=Process(1234,55)
>>>p2=Process(4544,34)

>>>class JobQueue:

     queue=[]
     def insert(p): #how can I insert a certain object that I created before (p1 or p2)?
     queue.append([p.pid,p.procTime])
如果能帮上点忙,我将不胜感激

编辑:下面是一些代码最后应该做的示例:

>>>p1=Process(5234,60)
>>>p2=Process(8824,17)
>>>p2=Process(2291,34)
>>>j=JobQueue()
>>>j.extractMin() #Giving me the minimum of the queue and deletes it. If it is empty, the output shall be float('inf')
inf
>>>j.insert(p1) #the method insert inserts the objects to the queue
>>>j.insert(p3)
>>>print(j.minimum()) #prints the ID with the shortest processing time of all objects
2291
>>>j.decreaseKey(p1,23) #replaces the current processing time of p1 by 23
>>>print(j.minimum())
5234
>>>j.insert(p2)
>>>print(j.extractMin())
8824
>>>print(j.extractMin())
5234
>>>print(j.minimum())
2291

我想这些例子已经足够让您了解我想写什么样的代码了。

好吧,您已经差不多做到了。只需将对象作为参数传递给方法。也就是说,您将
进程
对象传递给
JobQueue.insert
(或者传递给从该类创建的对象)。但是,定义方法时,第一个参数总是
self
。因此,你必须这样写:

class JobQueue:
    def __init__(self):
        self.queue=[] # This gives a unique queue for each object

    def insert(self, p):
        self.queue.append([p.pid,p.procTime])

也可以考虑在存储队列中存储<代码>进程<代码>对象>代码> p>代码>,而不是它的不同部分:

    def insert(self, p):
        self.queue.append(p)

这样,您就可以访问所有的
进程
对象,包括更新进程时间的函数。

好吧,您已经差不多做到了。只需将对象作为参数传递给方法。也就是说,您将
进程
对象传递给
JobQueue.insert
(或者传递给从该类创建的对象)。但是,定义方法时,第一个参数总是
self
。因此,你必须这样写:

class JobQueue:
    def __init__(self):
        self.queue=[] # This gives a unique queue for each object

    def insert(self, p):
        self.queue.append([p.pid,p.procTime])

也可以考虑在存储队列中存储<代码>进程<代码>对象>代码> p>代码>,而不是它的不同部分:

    def insert(self, p):
        self.queue.append(p)

通过这种方式,您可以访问所有
进程
对象,包括更新进程时间的函数。

您可以在作业队列的init方法中插入:

class Process:

    def __init__(self,pid,procTime):
        self.pid=str(pid)
        self.procTime=procTime



class JobQueue:

    def __init__(self,procs = None):

        self.queue = []
        for p in procs:
            self.insert(p)

    def insert(p):
        self.queue.append([p.pid,p.procTime])

p1=Process(1234,55)
p2=Process(4544,34)
a = JobQueue(procs = [p1, p2])

您可以在作业队列的init方法中插入:

class Process:

    def __init__(self,pid,procTime):
        self.pid=str(pid)
        self.procTime=procTime



class JobQueue:

    def __init__(self,procs = None):

        self.queue = []
        for p in procs:
            self.insert(p)

    def insert(p):
        self.queue.append([p.pid,p.procTime])

p1=Process(1234,55)
p2=Process(4544,34)
a = JobQueue(procs = [p1, p2])
大概是这样的:

class Process:
    def __init__(self,pid,procTime):
        self.pid=str(pid)
        self.procTime=procTime

class JobQueue:
    queue=[]
    def insert(self,p): 
        self.queue.append([p.pid,p.procTime])

p = Process(1,2)
jq=JobQueue()
jq.insert(p)
print (jq.queue)
OP:

[['1', 2]]
大概是这样的:

class Process:
    def __init__(self,pid,procTime):
        self.pid=str(pid)
        self.procTime=procTime

class JobQueue:
    queue=[]
    def insert(self,p): 
        self.queue.append([p.pid,p.procTime])

p = Process(1,2)
jq=JobQueue()
jq.insert(p)
print (jq.queue)
OP:

[['1', 2]]

这当然不是我想写的全部代码,有许多方法需要添加。我只写下了我现在的困境。
JobQueue
是静态的吗?为什么不在
过程中添加
队列
。\uuuu init\uuuu
?这当然不是我想写的全部代码,有许多方法需要添加。我只写下了我现在的困境。
JobQueue
是静态的吗?为什么不在
过程中添加到
队列
。\uuuu init\uuuu
?谢谢您的回复。我补充了一些例子。我想做的是把所有的对象放在一个队列中,这样比较它们会更容易,不是更有意义吗?我刚开始使用类和对象,还不熟悉。存储流程对象p而不是它的不同部分是什么意思(我知道你说的是什么,但是如何存储?)我已经更新了示例,展示了如何存储对象,或者更确切地说,是对它们的引用。哦,好的,我可以像任何常用项一样附加对象。谢谢但是关于我需要编码的内容,我应该为每个对象指定一个唯一的队列吗?您很可能只有一个
JobQueue
,这意味着在实践中,它是否唯一并不重要。谢谢您的回复。我补充了一些例子。我想做的是把所有的对象放在一个队列中,这样比较它们会更容易,不是更有意义吗?我刚开始使用类和对象,还不熟悉。存储流程对象p而不是它的不同部分是什么意思(我知道你说的是什么,但是如何存储?)我已经更新了示例,展示了如何存储对象,或者更确切地说,是对它们的引用。哦,好的,我可以像任何常用项一样附加对象。谢谢但是关于我需要编码什么,我应该为每个对象指定一个唯一的队列吗?您很可能只有一个
JobQueue
,这意味着在实践中,它是否唯一并不重要。