前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java中请给出一个return this的例子

java中请给出一个return this的例子

作者头像
马克java社区
修改2021-03-15 10:14:06
3560
修改2021-03-15 10:14:06
举报
文章被收录于专栏:java大数据

【新手可忽略不影响继续学习】下面例子中setYear中的return this;返回了一个指向对象的指针,this.setMonth(8).setDay(20);是合法的,如果像原来的例子一样什么都不返回,就成了void.setMonth(8).setDay(20); 马克-to-win,系统就该报错了

 

class MyTestDate {

    int year;

    int month;

    MyTestDate(int year, int month, int day) {

        this.year = year;

        this.month = month;

    }

    MyTestDate setYear(int year) {

        this.year = year;

        return this;

    }

    public MyTestDate setMonth(int month) {

        this.month = month;

        return this;

    }

    public String toString() {

        return "" + year + "/" + month  ;

    }

}

public class Test {

    public static void main(String[] args) {

        MyTestDate date = new MyTestDate(2009, 7, 18);

        System.out.println(date);

        date.setYear(2009).setMonth(8);

        System.out.println(date);

        MyTestDate date1 = new MyTestDate(2009, 1, 1);

        System.out.println(date1);

        date1.setYear(2006).setMonth(6);

        System.out.println(date1);

    }

}

 

更多请看:https://blog.csdn.net/qq_44639795/article/details/103129514

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档