this使用的两种情况
来源:互联网 发布:cf刷点卷软件 编辑:程序博客网 时间:2024/05/16 08:48
一、this关键字对于将当前对象传递给其他方法
/* * this关键字对于将当前对象传递给其他方法 */class Person {public void eat(Apple apple){Apple peeled=apple.getPeeled();System.out.println("eat...");}} class Apple {Apple getPeeled(){/** this关键字对于将当前对象传递给其他方法,Apple需要调用Peeler.peel()方法,它是一个外部工具类方法,* 将执行由于某种原因而必须放在Apple外部的操作。为了将其自身传递给外部方法,Apple必须使用this关键字*/return Peeler.peel(this);} } class Peeler {static Apple peel(Apple apple){return apple;}} public class PassingThis {public static void main(String[] args) {new Person().eat(new Apple());}}
二、返回对当前对象的引用
/** * 返回当前对象的引用 * @author xieyongxue * */public class Leaf {int i=0;Leaf increment(){i++;return this;}void print(){System.out.println("i="+i);}public static void main(String[] args) {Leaf leaf=new Leaf();leaf.increment().increment().increment().print();/** 输出值:i=3*/}}
阅读全文
0 0
- this使用的两种情况
- javascript的this的几种使用情况
- java中this的使用情况
- Sqlserver 中必须使用分号的两种情况--【叶子】
- Sqlserver 中必须使用分号的两种情况
- Sqlserver 中必须使用分号的两种情况--【叶子】
- Android四大组件之Service的两种使用情况
- Vue中使用vux的配置,分为两种情况:
- 两种情况的splitter
- git 两种情况的
- 继承父类的两种情况
- ExecuteScalar为空的两种情况
- 用到extern "C"的两种情况
- row lock waits的两种情况
- hibernate一对一的两种情况
- OpenERP采购的两种不同情况
- DexIndexOverflowException两种情况的解决方法
- OpenERP采购的两种不同情况
- java常用的几种数据结构,堆栈,队列,数组,链表,哈希表
- 继承
- 用两个栈实现一个队列算法
- 【算法】求连续子数组的最大和
- 简单谈谈JS数组中的indexOf方法
- this使用的两种情况
- 微信小程序----常用组件
- Maven_插件和私服
- 逻辑斯谛(Logistic)回归
- Some projects cannot be imported because they already exist in the workspace
- 泛型以及泛型怎么实现总结?
- 由浅入深之Tensorflow(3)----数据读取之TFRecords
- 分割字符串(有一个字符串,其中包含中文字符、英文字符和数字字符, * 请统计和打印出各个字符的个数)
- vim 常用命令