final int seed = 5; 内部匿名类//生成数组,具体生成方式取决于IntArrayProductor接口的匿名实现类
2013-01-01 17:13
393 查看
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] 请按任意键继续. . . */
相关文章推荐
- Mybatis使用Mapper代理的方式生成DAO接口的实现类对象
- 内部类实现方式的比较(匿名内部类,局部内部类)
- prim算法构造最小生成树(邻接表和数组两种存储方式实现)
- 泛型接口的实现方式二:直接在接口中指定具体类型
- ,有一款RESTFUL接口的文档在线自动生成+功能测试功能软件——Swagger UI,具体配置过程可移步《Spring Boot 利用 Swagger 实现restful测试》
- 一个int数组, 比如 array[],里面数据无任何限制,要求求出 所有这样的数array[i],其左边的数都小于等于它,右边的数都大于等于它。能否只用一个额外数组和少量其它空间实现
- android应用内部实现生成桌面快捷方式与进度条控制音量大小
- 泛型编程(模板函数,模板类的套用) Myvector 具体案例 实现可存放int 数组 char数组 类对象数组 以及一组指针
- 【匿名内部类】通过内部类匿名实现接口
- new 接口{内部实现方法} 转为 匿名内部类
- PHP的array_map函数使用类内部方法作为回调函数的实现方式
- 从零开始学Android之监听器实现监听动作的三种方式(匿名内部类,独立类,接口方式)
- vector 容器内部的实现(数组)具体实现参考boost库在另一篇文章
- Java 调用C# webservice接口 生成java客户端 实现方式
- Array数组接口实现的一些问题
- PHP删除数组空白元素的具体实现方式分享
- handler和实现接口的方式实现按钮的点击功能
- 当把属性的语义特性分别声明为assign,retain,copy时, 自动生成setter, getter方法内部实现
- php实现根据字符串生成对应数组的方法
- java中byte数组与int类型的转换(两种方式)