package com.test1.www;

class NutritionFacts {
//必须
private int servingSize;
private int servings;
//可选, Build类必须初始化
private int calories;
private int fat;
private int sodium;
private int carboohydrate; public int getServingSize() {
return servingSize;
} public int getServings() {
return servings;
} public int getCalories() {
return calories;
} public int getFat() {
return fat;
} public int getSodium() {
return sodium;
} public int getCarboohydrate() {
return carboohydrate;
} static class Builder {
private int servingSize;
private int servings;
private int calories=0;
private int fat=0;
private int sodium=0;
private int carboohydrate=0; public Builder(int servingSize, int servings) {
this.servingSize = servingSize;
this.servings = servings;
} public Builder calories(int val) {
calories = val;
return this;
} public Builder fat(int val) {
fat = val;
return this;
} public Builder sodium(int val) {
sodium = val;
return this;
} public Builder carboohydrate(int val) {
carboohydrate = val;
return this;
}
public NutritionFacts build() {
return new NutritionFacts(this);
} } public NutritionFacts(Builder builder){
servingSize = builder.servingSize;
calories=builder.calories;
fat=builder.fat;
sodium = builder.sodium;;
carboohydrate= builder.carboohydrate; }
} public class Demo2 {
public static void main(String[] args) {
NutritionFacts nt = new NutritionFacts.Builder(240, 8).calories(20).carboohydrate(30).fat(20).sodium(2).build(); System.out.println(nt.getServingSize());
System.out.println(nt.getServings());
System.out.println(nt.getCalories());
System.out.println(nt.getFat());
System.out.println(nt.getCarboohydrate());
System.out.println(nt.getSodium());
}
}

java_可变参数构造器 Bulder模式的更多相关文章

  1. Java_可变参数类型

    Java方法中的可变参数类型,也称为不定参数类型,是一个非常重要的概念 举栗子 public class TestVarArgus { public static void dealArray(int ...

  2. 大数据学习day13------第三阶段----scala01-----函数式编程。scala以及IDEA的安装,变量的定义,条件表达式,for循环(守卫模式,推导式,可变参数以及三种遍历方式),方法定义,数组以及集合(可变和非可变),数组中常用的方法

    具体见第三阶段scala-day01中的文档(scala编程基础---基础语法)  1. 函数式编程(https://www.cnblogs.com/wchukai/p/5651185.html): ...

  3. 黑马程序员——【Java高新技术】——JDK1.5新特性:静态导入、可变参数、增强型for循环、自动装箱拆箱、枚举

    ---------- android培训.java培训.期待与您交流! ---------- 一.静态导入 1.import和import static区别: (1)import 是导入一个类或某个包 ...

  4. java-API中的常用类,新特性之-泛型,高级For循环,可变参数

    API中的常用类 System类System类包含一些有用的类字段和方法.它不能被实例化.属性和方法都是静态的. out,标准输出,默认打印在控制台上.通过和PrintStream打印流中的方法组合构 ...

  5. JAVA基础学习之IP简述使用、反射、正则表达式操作、网络爬虫、可变参数、了解和入门注解的应用、使用Eclipse的Debug功能(7)

    1.IP简述使用//获取本地主机ip地址对象.InetAddress ip = InetAddress.getLocalHost();//获取其他主机的ip地址对象.ip = InetAddress. ...

  6. [Effective JavaScript 笔记]第22条:使用arguments创建可变参数的函数

    第21条讲述使用可变参数的函数average.该函数可处理任意数量的参数并返回这些参数的平均值. 如何创建可变参数的函数 1.实现固定元数的函数 书上的版本 function averageOfArr ...

  7. CCS5 编译器手动设置dsp支持可变参数宏等问题

    IDE:CSS5.4,compiler不支持可变参数宏.需要手动设置编译器相关选项: Language Option->Language Mode —>no strict ANSI. 1. ...

  8. Effective Java 第三版——53. 明智而审慎地使用可变参数

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  9. .NET框架- in ,out, ref , paras使用的代码总结 C#中in,out,ref的作用 C#需知--长度可变参数--Params C#中的 具名参数 和 可选参数 DEMO

    C#.net 提供的4个关键字,in,out,ref,paras开发中会经常用到,那么它们如何使用呢? 又有什么区别? 1 in in只用在委托和接口中: 例子: 1 2 3 4 5 6 7 8 9 ...

随机推荐

  1. apache开源项目--HBase

    HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. HBase是Goog ...

  2. 【转】Android异步消息处理机制完全解析,带你从源码的角度彻底理解

    原文网址:http://blog.csdn.net/guolin_blog/article/details/9991569 转载请注明出处:http://blog.csdn.net/guolin_bl ...

  3. 9.19AD和DA操作

    下载芯片说明书的网站:http://www.21ic.com/ D/A digital是数字信号,analog是模拟信号,单片机属于数字芯片,内部只有0和1,这两种信息无法表示一个模拟量,如果是一个8 ...

  4. 《转》GDB中应该知道的几个调试方法

    原文:http://coolshell.cn/articles/3643.html 七.八年前写过一篇<用GDB调试程序>,于是,从那以后,很多朋友在MSN上以及给我发邮件询问我关于GDB ...

  5. NOIP算法总结

    前言 离NOIP还有一个星期,匆忙的把寒假整理的算法补充完善,看着当时的整理觉得那时还年少.第二页贴了几张从贴吧里找来的图片,看着就很热血的.旁边的同学都劝我不要再放PASCAL啊什么的了,毕竟我们的 ...

  6. 取小数点后三位的方法(js)

    使用js取小数点后三位的方法,我觉得这个方法最好 Math.round(num*100)/100 还有其他方法: http://hi.baidu.com/yansueh/item/f026d8d759 ...

  7. 【暑假】[深入动态规划]UVa 1380 A Scheduling Problem

     UVa 1380 A Scheduling Problem 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=41557 ...

  8. Discuz资料整理

    1.截取字符串:messagecutstr(strip_tags($post['message']), 160);

  9. HDU-4690 EBCDIC 映射,模拟,沙茶

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4690 纯沙茶模拟题... //STATUS:C++_AC_93MS_228KB #include &l ...

  10. 把存储过程获取的数据输出到报表的html模板中

    制作报表的html模板 <HTML><meta http-equiv="Content-Type" content="text/html; charse ...