Recursive Generic Usage

后端 未结 4 1760
甜味超标
甜味超标 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:35

    You can make generic methods as well as generic types. Using these, the problem methods in GraphUtils can be fixed like this:

    static > void addNewNeighbors1a(T node, T newNode)
    {
      node.addNeighbor(newNode);
      print("Add city to node", node.neighbors());
    }
    
    static > void addNewNeighbors2(T node, T newNode)
    {
      node.addNeighbor(newNode);
      print("Add concrete node to node", node.neighbors());
    }
    

    Hey, wait a second… those are the same methods!

    It turns out that, since they only depend on the interface of Node, you only need one of them to handle any Node implementation.

    Down the road, you might find it necessary to change the Node interface like this:

    public abstract  void addNeighbor(S n);
    

提交回复
热议问题