python - 如何从其他对象访问一个对象
问题描述
我正在解决一些问题,尽管在这个问题上花了很多时间,但我找不到答案,所以这是我的问题:
我有 2 个类,我们称它们为 Room 和 Vacuum。房间里有脏瓷砖,真空吸尘器会清理它。房间的实例作为参数传递给真空实例。
问题是:如何初始化指向房间实例的“指针”?我的目标是能够让多个真空物体同时“清洁”同一个房间。据我所知,如果我在真空 __init __ 中使用 self.room = room,这将在真空的每个实例内创建一个新对象,而不是将现有房间链接到所有真空。
在建议之前:我无法更改类参数,因为这些是固定的要求。
编辑:示例代码:
class Room(object):
"""
A Room represents a rectangular region containing clean or dirty
tiles.
A room has a width and a height and contains (width * height) tiles. At any
particular time, each of these tiles is either clean or dirty.
"""
def __init__(self, width, height):
"""
Initializes a room with the specified width and height.
Initially, no tiles in the room have been cleaned.
width: an integer > 0
height: an integer > 0
"""
self.width = width
self.height = height
self.clean_tiles = []
self.dirty_tiles = []
for i in range(width):
for j in range(height):
self.dirty_tiles.append((i,j))
...
def cleanTileAtPosition(self, pos):
"""
Mark the tile under the position POS as cleaned.
Assumes that POS represents a valid position inside this room.
pos: a Position
"""
tile = (int(pos.getX()), int(pos.getY()))
if tile in self.dirty_tiles:
self.dirty_tiles.remove(tile)
self.clean_tiles.append(tile)
class Vacuum(object):
"""
Represents a robot cleaning a particular room.
At all times the robot has a particular position and direction in the room.
The robot also has a fixed speed.
Subclasses of Robot should provide movement strategies by implementing
updatePositionAndClean(), which simulates a single time-step.
"""
def __init__(self, room, speed):
"""
Initializes a Robot with the given speed in the specified room. The
robot initially has a random direction and a random position in the
room. The robot cleans the tile it is on.
room: a RectangularRoom object.
speed: a float (speed > 0)
"""
self.room = room
self.speed = speed
self.direction = random.randint(0, 360)
self.pos = Position(random.uniform(0, self.room.width), random.uniform(0,room.height))
self.room.cleanTileAtPosition(self.pos)
...
and the subclass of Vacuum, that will actually be used to create instances:
class StandardVacuum(Vacuum):
"""
A StandardVacuum is a Vacuum with the standard movement strategy.
At each time-step, a StandardRobot attempts to move in its current
direction; when it would hit a wall, it *instead* chooses a new direction
randomly.
"""
def updatePositionAndClean(self):
"""
Simulate the passage of a single time-step.
Move the robot to a new position and mark the tile it is on as having
been cleaned.
"""
# print("current pos: ", self.pos)
new_pos = self.pos.getNewPosition(self.direction, self.speed)
while self.room.isPositionInRoom(new_pos) == False:
self.setRobotDirection(random.randint(0, 360))
# print("direction: ", self.direction)
new_pos = self.pos.getNewPosition(self.direction, self.speed)
# print("new pos is:", new_pos)
self.setRobotPosition(new_pos)
self.room.cleanTileAtPosition(self.pos)
解决方案
推荐阅读
- html - 如何使用 fullpage.js 在移动设备上从 3 张幻灯片制作 9 张幻灯片?
- swift - 在 swift 4 或更高版本上创建触摸测试应用程序
- reactjs - 如何使用 React PWA 像原生 iOS 应用一样进行导航转换?
- java - 没有 web.xml 的 JNDI 参考和查找资源
- javascript - 扩展 ng2-pdf-viewer 以支持在 pdf 之上绘图的方法?
- java - 如何使用 Jackson 和 spring-boot 将简单的 Kotlin 数据类映射到/从 json
- web - 从 Web 向 Ionic3 发送推送通知
- php - TCPDF 字体工具:如何使用 OTF 字体类型生成字体文件
- mysql - 限制 SQL 查询的某些属性
- mysql - 触发器内不同条件选择的多个 IF