使用类对象作为参数

时间:2016-09-25 02:58:08

标签: python

我很担心如何处理这个问题。我想在x类中添加yCar坐标,如

class Car:
    def __init__(self,x,y):
        self.x = x
        self.y = y

然后我需要在dict _cars中的键中实现这些参数,但由于'id_'的键是字符串,我不能做类似的事情:

temp = Car(0,0)
self._cars[temp] = fuel

我该如何处理?

class SuperDuperManager:
    """A class responsible for keeping track of all cars in the system.

    === Private Attributes ===
    @type _cars: dict[str, Car]
        A map of unique string identifiers to the corresponding Car.
        For example, _cars['a01'] would be a Car object corresponding to
        the id 'a01'.
    """
    def __init__(self):
        """Initialize a new SuperDuperManager.

        There are no cars in the system when first created.

        @type self: SuperDuperManager
        @rtype: None
        """
        self._cars = {}

    def add_car(self, id_, fuel):
        """Add a new car to the system.

        The new car is identified by the string <id_>, and has initial amount
        of fuel <fuel>.

        Do nothing if there is already a car with the given id.

        @type self: SuperDuperManager
        @type id_: str
        @type fuel: int
        @rtype: None
        """
        # Check to make sure the identifier isn't already used.
        if id_ not in self._cars:
            # TODO: Add the new car.
            if self._cars[id_] = fuel

    def move_car(self, id_, new_x, new_y):
        """Move the car with the given id.

        The car called <id_> should be moved to position (<new_x>, <new_y>).
        Do nothing if there is no car with the given id,
        or if the corresponding car does not have enough fuel.

        @type self: SuperDuperManager
        @type id_: str
        @type new_x: int
        @type new_y: int
        @rtype: None
        """
        if id_ in self._cars:
            # TODO: Move the car with id <id_>.
            if self._cars[id_] >= (new_x + new_y):
                    remaining_fuel = self._cars[id] - (new_x + new_y)
                    self._cars[id_] = Car(new_x, new_y, remaining_fuel)

    def get_car_position(self, id_):
        """Return the position of the car with the given id.

        Return a tuple of the (x, y) position of the car with id <id_>.
        Return None if there is no car with the given id.

        @type self: SuperDuperManager
        @type id_: str
        @rtype: (int, int) | None
        """
        if id_ in self._cars:
            # TODO: Get the position of the car with id <id_>.
            return (self._cars[id_].x, self._cars[id_].y)

    def get_car_fuel(self, id_):
        """Return the amount of fuel of the car with the given id.

        Return None if there is no car with the given id.

        @type self: SuperDuperManager
        @type id_: str
        @rtype: int | None
        """
        if id_ in self._cars:
            # TODO: Get the amount of fuel of the car with id <id_>.
            return (self._cars[id_].fuel)

    def dispatch(self, x, y):
        """Move a car to the given location.

        Choose a car to move based on the following criteria:
        (1) Only consider cars that *can* move to the location.
            (Ignore ones that don't have enough fuel.)
        (2) After (1), choose the car that would move the *least* distance to
            get to the location.
        (3) If there is a tie in (2), pick the car whose id comes first
            alphabetically. Use < to compare the strings.
        (4) If no cars can move to the given location, do nothing.

        @type self: SuperDuperManager
        @type x: int
        @type y: int
        @rtype: None
        """
        # TODO: Implement this method!
        pass

class Car:
    """A car in the Super system.
    """
    def __init__(self, x, y, fuel):
            self.x = x
            self.y = y
            self.fuel = fuel

0 个答案:

没有答案