java return this 什么意思,应该怎么用!~
答案:1 悬赏:20 手机版
解决时间 2021-03-15 01:03
- 提问者网友:我的未来我做主
- 2021-03-14 04:00
最好能有一个小例子,谢谢了
最佳答案
- 五星知识达人网友:第幾種人
- 2021-03-14 04:09
连续操作类里方法,每次返回类实例,用return this;
class Test2 {
String s="";
public static void main(String[] args) {
Test2 t = new Test2();
t.method("111");
t.method("222");
t.method("333");
t.method2();
}
public Test2 method(String sb) {
s+=sb;
return this;
}
public void method2() {
System.out.println(s);
}
} 结果: 111222333
class Test2 {
String s="";
public static void main(String[] args) {
Test2 t = new Test2();
t.method("111");
t.method("222");
t.method("333");
t.method2();
}
public Test2 method(String sb) {
s+=sb;
return this;
}
public void method2() {
System.out.println(s);
}
} 结果: 111222333
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯