打字猴:1.700468622e+09
1700468622
1700468623 代码清单21-23 树叶节点
1700468624
1700468625 public class Leaf extends Component{
1700468626
1700468627 @Deprecated
1700468628
1700468629 public void add(Component component)throws UnsupportedOperationException{
1700468630
1700468631 //空实现,直接抛弃一个”不支持请求”异常
1700468632
1700468633 throw new UnsupportedOperationException();
1700468634
1700468635 }
1700468636
1700468637 @Deprecated
1700468638
1700468639 public void remove(Component component)throws UnsupportedOperationException{
1700468640
1700468641 //空实现
1700468642
1700468643 throw new UnsupportedOperationException();
1700468644
1700468645 }
1700468646
1700468647 @Deprecated
1700468648
1700468649 public ArrayList<Component>getChildren()throws UnsupportedOperationException{
1700468650
1700468651 //空实现
1700468652
1700468653 throw new UnsupportedOperationException();
1700468654
1700468655 }
1700468656
1700468657 }
1700468658
1700468659 为什么要加个Deprecated注解呢?就是在编译器期告诉调用者,你可以调我这个方法,但是可能出现错误哦,我已经告诉你“该方法已经失效”了,你还使用那在运行期也会抛出UnsupportedOperationException异常。
1700468660
1700468661 在透明模式下,遍历整个树形结构是比较容易的,不用进行强制类型转换,如代码清单21-24所示。
1700468662
1700468663 代码清单21-24 树结构遍历
1700468664
1700468665 public class Client{
1700468666
1700468667 //通过递归遍历树
1700468668
1700468669 public static void display(Component root){
1700468670
1700468671 for(Component c:root.getChildren()){
[ 上一页 ]  [ :1.700468622e+09 ]  [ 下一页 ]