Recursive Generic Usage

后端 未结 4 1759
甜味超标
甜味超标 2020-12-10 19:27

Edited:\" I received a very pertinent answer from \'erickson\', but there is a side problem (up-casting?) that was not explicitly covered in my original

4条回答
  •  情话喂你
    2020-12-10 19:57

    For part 1, I would make the method signiture

    static > void addNewNeighbors(Node node, T newNode)
    {
      node.addNeighbor(newNode);
    }
    

    That way, node does not have to specifically be the same type as newNode.

    For part 2, I would do something similar.

    ConcreteNode nc = new City("");
    nc.addNeighbor(new City("Bern"));
    

提交回复
热议问题