这是我第一次在这里发帖,而且我是 Python 新手,所以如果我有什么问题,请耐心等待!我已经看到了与此类似的问题,但看不到明确的解决方案。这必须有一个简单的解决方案,但它让我整天感到沮丧。
我正在尝试在“Board”类的构造函数中创建“Slot”类的多个对象。制作每个对象后,我将其附加到列表中。最后,我将遍历列表中的每个对象并调用“getHandles”方法,该方法应该输出它们独特的结果。而是显示最后一个槽对象的结果。
板类构造函数:
class Board:
slots = []
def __init__(self):
slot1 = Slot(1); self.slots.append(slot1);
slot2 = Slot(2); self.slots.append(slot2);
slot3 = Slot(3); self.slots.append(slot3);
slot4 = Slot(4); self.slots.append(slot4);
slot5 = Slot(5); self.slots.append(slot5);
slot6 = Slot(6); self.slots.append(slot6);
slot7 = Slot(7); self.slots.append(slot7);
slot8 = Slot(8); self.slots.append(slot8);
slot9 = Slot(9); self.slots.append(slot9);
print("\nprinting slot objects in list 'slot'\n");
counter = 0;
while counter < len(self.slots):
print("slot number :"+str(self.slots[counter].getPosition()));
print("slot value :"+str(self.slots[counter].getHandles()));
counter +=1;
插槽类构造函数:
#Slot class constructor
def __init__(self,n):
self.setPosition(n);
positions = coords.getLargeGridPosition(n);
self.U[2] = positions[1]; self.D[2] = positions[2];
self.L[2] = positions[3]; self.R[2] = positions[4];
print("made slot object : "+str(self.getPosition()));
print("handle values: ");
self.getHandles();
print(" - ");
def getHandles(self):
print(" U:"+str(self.U)+" D:"+str(self.D)+" L:"+str(self.L)+" R:"+str(self.R));
这就是我制作“Board”对象时得到的结果:
第一部分输出(制作插槽并在 Board 构造函数中附加):
>>> b = Board()
made slot object : 1
handle values:
U:[1, 2, 2] D:[2, 1, 20] L:[3, 4, 10] R:[4, 3, 12]
-
made slot object : 2
handle values:
U:[1, 2, 5] D:[2, 1, 23] L:[3, 4, 13] R:[4, 3, 15]
-
made slot object : 3
handle values:
U:[1, 2, 8] D:[2, 1, 26] L:[3, 4, 16] R:[4, 3, 18]
-
made slot object : 4
handle values:
U:[1, 2, 29] D:[2, 1, 47] L:[3, 4, 37] R:[4, 3, 39]
-
对于所有九个对象,依此类推。第二部分输出(为列表中的每个对象调用 getHandle 方法):
printing slot objects in list 'slot'
slot number :1
U:[1, 2, 62] D:[2, 1, 80] L:[3, 4, 70] R:[4, 3, 72]
slot value :None
slot number :2
U:[1, 2, 62] D:[2, 1, 80] L:[3, 4, 70] R:[4, 3, 72]
slot value :None
slot number :3
U:[1, 2, 62] D:[2, 1, 80] L:[3, 4, 70] R:[4, 3, 72]
slot value :None
slot number :4
U:[1, 2, 62] D:[2, 1, 80] L:[3, 4, 70] R:[4, 3, 72]
slot value :None
slot number :5
U:[1, 2, 62] D:[2, 1, 80] L:[3, 4, 70] R:[4, 3, 72]
对附加到列表的所有 9 个对象依此类推。如何使变量 slot1/slot2 引用单个对象?
我尝试将板对象传递给插槽构造函数,并使插槽构造函数将“自我”传递回板对象。仍然会为所有人输出单个对象的结果。我会很感激任何帮助。谢谢你。
[编辑] 文件的完整代码:希望缩进正确
import coords;
#store hanndle position values
class Slot: #slot class<------------------------------------------------
U = [1,2,"gridPosition"] #1 up
D = [2,1,"gridPosition"] #2 down
L = [3,4,"gridPosition"] #3 left
R = [4,3,"gridPosition"] #4 right
handles = [U,D,L,R] #handles 0/1/2/3
tileValue = 0; #value 0 - 8
tilePosition = 0; #slot 1 - 9
overallStrain = 0; # |0-->+
#Slot class constructor
def __init__(self,n):
self.setPosition(n);
positions = coords.getLargeGridPosition(n);
self.U[2] = positions[1]; self.D[2] = positions[2];
self.L[2] = positions[3]; self.R[2] = positions[4];
print("made slot object : "+str(self.getPosition()));
print("handle values: ");
self.getHandles();
print(" - ");
def getHandles(self):
print(" U:"+str(self.U)+" D:"+str(self.D)+" L:"+str(self.L)+" R:"+str(self.R));
#set/get position of slot(1-9)
def setPosition(self,position):
self.tilePosition = position;
def getPosition(self):
return self.tilePosition;
#set/get tile value
def setTileValue(self,value):
self.TileValue = value;
def getTileValue(self):
return self.TileValue;
#get handle(n) position value
def getHandle(self,number):
return self.handles[number]
#get overall strain of tile
def getTotalStrain(self):
return self.totalStrain;
#calculate total strain from strains of handles
#def calculateOverallStrain(self):#<---------------
#for handle in handles:
#if
#get linked tile,
class Board: #Boardclass<------------------------------------------------
slots = []
def __init__(self):
slot1 = Slot(1); self.slots.append(slot1);
slot2 = Slot(2); self.slots.append(slot2);
slot3 = Slot(3); self.slots.append(slot3);
slot4 = Slot(4); self.slots.append(slot4);
slot5 = Slot(5); self.slots.append(slot5);
slot6 = Slot(6); self.slots.append(slot6);
slot7 = Slot(7); self.slots.append(slot7);
slot8 = Slot(8); self.slots.append(slot8);
slot9 = Slot(9); self.slots.append(slot9);
print("\nprinting slot objects in list 'slot'\n");
counter = 0;
while counter < len(self.slots):
print("slot number :"+str(self.slots[counter].getPosition()));
print("slot value :"+str(self.slots[counter].getHandles()));
counter +=1;
#set tiles
def setTiles(self,state):
counter = 0;
for slot in self.slots:
slot.setTileValue(state[counter]);
counter +=1;
def getStrain(self,slot):
tileValue = self.slots[slot].getTileValue();
print("tile value:"+str(tileValue));
print("handles:");
self.slots[slot].getHandles();
if tileValue != 0:
linkedTiles = coords.getLinked(tileValue);
print(str(linkedTiles));
counter = 0;
for l in linkedTiles:#for each linked tile
if l != 0: #if not empty link
linkedTileSlot = [];#slot linked tile is set in
for slot in self.slots:
linkedTileValue = slot.getTileValue();
print(str(linkedTileValue));
if l == linkedTileValue:#found slot with linked tile
print("yes");
linkedTileSlot = slot; break;
ownHandle = slot.getHandle(counter);
print("own handle : "+str(ownHandle));
linkingHandle = linkedTileSlot.getHandle(ownHandle[1]-1);
print("linking handle : "+str(linkingHandle));
positions = [ownHandle[2],linkingHandle[2]]
print(str(positions));
counter += 1;