final int seed = 5; 内部匿名类//生成数组,具体生成方式取决于IntArrayProductor接口的匿名实现类

来源:互联网 发布:javascript 面向对象 编辑:程序博客网 时间:2024/06/06 01:31
import java.util.*;/** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>  * <br/>Copyright (C), 2001-2010, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author  Yeeku.H.Lee kongyeeku@163.com * @version  1.0 */interface IntArrayProductor{//接口里定义的product方法用于封装“处理行为”int product();}public class CommandTest{//定义一个方法,该生成指定长度的数组,但每个数组元素由于cmd负责产生public int[] process(IntArrayProductor cmd  , int length) {int[] result = new int[length];for (int i = 0; i < length ; i++ ){result[i] = cmd.product();}return result;}public static void main(String[] args) {CommandTest ct = new CommandTest();final int seed = 5;//生成数组,具体生成方式取决于IntArrayProductor接口的匿名实现类int[] result = ct.process(new IntArrayProductor(){public int product(){return (int)Math.round(Math.random() * seed);}} , 6);System.out.println(Arrays.toString(result));int testarry[]={4, 3, 4, 1, 0, 5};System.out.println(Arrays.toString(testarry));}}/*[0, 2, 1, 2, 1, 3][4, 3, 4, 1, 0, 5]请按任意键继续. . .*/

原创粉丝点击