[英]How to pass arguments to a child class
Basically I want to have 4 classes where I want to choose each of the four classes randomly according to a random number generator.The parent class has all the functions and I want to be able to have 3 other child classes to be the same function as the parent class基本上我想要有 4 个类,我想根据随机数生成器随机选择四个类中的每一个。父 class 具有所有功能,我希望能够有 3 个其他子类与 function 相同父 class
class Job:
......(all the code over here)....
class Job2(Job):
pass
class Job3(Job):
pass
class Job4(Job):
pass
for i in range 4:
a=randint(0,4)
if a==1:
Job
if a==2:
Job2
if a==3:
Job3
if a==4:
Job4
The whole point of this is to reduce the "waiting time" (this is basically a FIFO simulation) by a quarter.这样做的重点是将“等待时间”(这基本上是一个 FIFO 模拟)减少了四分之一。 I thought that by doing random and iterating it 4 times it would go over the Jobs 4 times in iteration我认为通过随机和迭代它 4 次它会 go 在 Jobs 上迭代 4 次
How do I call a Job in a function?如何在 function 中调用作业?
Can be refined, but main ideas on this are that when you implement __call__
as a method of a class, the instantiated object can be called as a function.可以改进,但主要思想是,当您将__call__
实现为 class 的方法时,实例化的 object 可以称为 function。
( jobs[a]()
instantiates the job, and then you call the result of this operation as a function). ( jobs[a]()
实例化作业,然后将此操作的结果作为函数调用)。
from random import randint
class Job:
def __init__(self, id=0):
self.id = id
def __call__(self):
print(self.id)
class Job1(Job):
def __init__(self):
self.id = 1
class Job2(Job):
def __init__(self):
self.id = 2
class Job3(Job):
def __init__(self):
self.id = 3
a=randint(0, 3)
jobs = [Job, Job1, Job2, Job3]
job = jobs[a]()
job()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.