在下述代码中,weightEdges数组中存放的是weightedEdge类型,当把weightedEdge数组赋给Listedges数组的时候,为什么是这样强转Listedges=(List)weightEdges?我的理解应该是将ListweightEdges中的每一个weightEdges对象向上转型成器父类edge。请大神解决一下疑问。代码部分:javaimportjava.util.ArrayList;importjava.util.List;publicclassProblem{publicstaticvoidmain(String[]args){ListweightEdges=newArrayList<>(); for(inti=0;i<10;i++)weightEdges.add(newweightedEdge(i,i,i));//TODO为何这样强转??????*Listedges=(List)weightEdges;* }}classedge{privateintu;privateintv;publicedge(intu,intv){super();this.u=u;this.v=v;}publicintgetU(){returnu;}publicintgetV(){returnv;}}classweightedEdgeextendsedge{privateintweight;publicweightedEdge(intu,intv,intweight){super(u,v);this.weight=weight;}publicintgetWeight(){returnweight;}}
慕哥9229398
相关分类