首页 > 编程语言 > 详细

java 连缀用法

时间:2019-08-11 10:59:37      阅读:117      评论:0      收藏:0      [点我收藏+]

 

连缀用法,即是在实例化对象时,同时为对象的属性设值。

如示例所示,在创建对象时,同时调用属性的设值函数,为属性赋值

        Apple apple = new Apple()
                .setColor("Green")
                .setWeight(12.3f);

 

实现方法是,每个设值函数都返回this

public class Apple {

    private String color;
    public Apple setColor(String color) {
        this.color = color;
        return this;
        }
    public String getColor() {return color;}

    private float weight;
    public Apple setWeight(float weight) {
        this.weight = weight;
        return this;
        }
    public float getWeight() {return weight;}

}

 

java 连缀用法

原文:https://www.cnblogs.com/deltadeblog/p/11333683.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!