zoukankan      html  css  js  c++  java
  • 适配器模式

     1 package com.jdk7.chapter2.adapter;
     2 /**
     3  * AdapterPrint适配器既继承了PrintIntArray的打印数组的功能,又实现了排序接口的排序功能SortNumber,
     4  * 把不相关的功能集合到一起
     5  * @author Administrator
     6  *
     7  */
     8 
     9 //需要使用不相关类的方法则继承该类即可继承该类的功能
    10 public class AdapterPrint extends PrintIntArray implements SortNumber{
    11     //将接口作为适配器的私有对象类型
    12     private SortNumber sort;
    13     //将适配器构造函数中对象类型的复件引用传给适配器中的私有对象变量
    14     public AdapterPrint(SortNumber sort1){
    15         this.sort = sort1;
    16     }
    17     @Override
    18     public int[] sortASCNumber(int[] intArray) {
    19         if(this.sort!=null){
    20             return this.sort.sortASCNumber(intArray);
    21         }else{
    22             return null;
    23         }
    24     }
    25     
    26     public static void main(String[] args) {
    27         int[] array = new int[] {5,8,7,6,1,4,3,2};
          //使用工厂模式创建排序接口对象
    28 AdapterPrint adapter = new AdapterPrint(Factory.getSortNumber(Factory.BUBBLE_SORT)); 29 adapter.printIntArray(adapter.sortASCNumber(array)); 30 } 31 }
  • 相关阅读:
    RabbitMQ:六、网络分区
    RabbitMQ:五、高阶
    RabbitMQ:四、跨越集群
    数据结构:红黑树
    RabbitMQ:三、进阶
    面对对象多态的异常
    面向对象三大特征---多态
    面对对象继承的优点和缺点
    面对对象当中的代码块
    面对对象this关键字
  • 原文地址:https://www.cnblogs.com/celine/p/8298062.html
Copyright © 2011-2022 走看看