首页 > 解决方案 > 如何从其他对象访问一个对象

问题描述

我正在解决一些问题,尽管在这个问题上花了很多时间,但我找不到答案,所以这是我的问题:

我有 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)

标签: pythonobjectreference

解决方案


推荐阅读