java-可变参数示例
2013-09-17 11:30
183 查看
package cd.itcast.day1; public class OrderItem { private Long id; private Double price; private Integer num; public OrderItem(Long id, Double price, Integer num) { super(); this.id = id; this.price = price; this.num = num; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Integer getNum() { return num; } public void setNum(Integer num) { this.num = num; } } package cd.itcast.day1; /** * 为了解决参数不固定性,引入了可变参数 * * 类型 ... 引用名称 * * @author Administrator * */ public class VarArgsDemo { public static void main(String[] args) { OrderItem item1 = new OrderItem(1l, 20d, 10); OrderItem item2 = new OrderItem(1l, 20d, 10); OrderItem item3 = new OrderItem(1l, 20d, 10); Double total = count(.8, new OrderItem[] { item1, item2, item3 }); System.out.println(total); } /** * [Lcd.itcast.day1.OrderItem;@1fb8ee3 * * @param items * 1,可变参数就是数组,在编译完成之后,方法的签名变成了数组的方式 2,可变参数必须放到参数列表的最后 * @return */ private static Double count(Double cutoff, OrderItem... items) { // 在1.4写法: // Double total = new Double(0); // Double total=Double.valueOf(0); /** * 这里存在大量的自动装箱/拆箱特性,这个特性也是编译器级别的特性 */ Double total = 0d; for (int i = 0; i < items.length; i++) { OrderItem item = items[i]; total += item.getPrice() * item.getNum(); } return total * cutoff; } private static Double sum(OrderItem[] items) { Double total = 0d; for (int i = 0; i < items.length; i++) { OrderItem item = items[i]; total += item.getPrice() * item.getNum(); } return total; } }
相关文章推荐
- java可变参数使用示例
- Java数组可变长参数详解
- Java数组可变长参数详解
- java 可变长参数
- Java中可变参数
- 09jdk5可变参数与OverLoad;10java5的增加for循环
- C#与Java对比学习:数据类型、集合类、栈与队列、迭达、可变参数、枚举
- 黑马程序员__JAVA高新技术--枚举及内省JavaBean、可变参数等知识点总结
- java可变参数
- JAVA中的可变参数
- java中形参的可变参数的定义(如String... args) .
- Java_可变参数
- Java 可变参数
- Java可变参数的使用
- java根据方法名称取得反射方法的参数类型示例
- 深入了解JAVA可变长度的参数
- java课堂练习之可变参数与卫条件
- JAVA类型后面跟3个点(可变长度参数列表)
- Java中可变长度参数代码详解
- Java方法的可变参数类型 int... i 的用法