i have two classes one is FirstClass and he second is SecondClass, i need to initialize my list of secondClass (type Secondclass) with a list of Firstclass. the main idea is not to make a specific value while calling main function but random values as per my function initSecondClass()calling the getters and setters from firstclass.
class Firstclass:
def __init__(self):
nom = None
val = int()
valMax = int()
valMin = int()
self._nom = nom
self._val = val
self._valMax = valMax
self._valMin = valMin
@property
def nom(self):
return self._nom
@nom.setter
def nom(self, nom):
self._nom = nom
@property
def val(self):
return self._val
@val.setter
def val(self, val):
self._val = val
@property
def valMax(self):
return self._valMax
@valMax.setter
def valMax(self, valMax):
self._valMax = valMax
@property
def valMin(self):
return self._valMin
@valMin.setter
def valMin(self, valMin):
self._valMin = valMin
self.setValMax(Max)
def __str__(self):
return "Firstclass { nom = " + self._nom + ", val = " + str(
self._val) + ", valMax = " + str(self._valMax) + ", valMin = " + str(self._valMin) + " }"
--------------------------
import random
import Firstclass
class Secondclass:
def __init__(self):
indiceI = int()
listeSecondclass = []
obj = float()
self._indiceI = indiceI
self.listeSecondclass = listeSecondclass
self._obj = obj
@property
def listeSecondclass(self):
return self._listeSecondclass
@listeSecondclass.setter
def listeSecondclass(self, listeSecondclass):
self.listeSecondclass = listeSecondclass
@property
def obj(self):
return self._obj
@obj.setter
def obj(self, obj):
self._obj = obj
def __str__(self):
return " Objet : " + str(
self._obj) + "\t liste Secondclass : " + str(self._listeSecondclass)
def initSecondclass(self):
i = 0
mylist = []
while i < len(self.listeSecondclass):
Max = self.listeSecondclass[i].valMax
Min = self.listeSecondclass[i].valMin
val = random.randint(Min, Max)
mylist.append(self.listeSecondclass[i].val)
i += 1
return mylist
def main():
firstclass = Firstclass.Firstclass()
secondClass = Secondclass()
secondClass.listeSecondclass = [firstclass]
secondClass.obj = 15.7
print(secondClass.__str__())
print(secondClass.initSecondclass())
if __name__ == '__main__':
main()
Aucun commentaire:
Enregistrer un commentaire