Java通过内部类实现回调功能
2013-04-23 11:20
239 查看
一个类,因为不方便直接修改内部的属性,所以可以通过回调的方式来进行。
下面是回调的方法:
上面的代码中,通过利用成员内部类可以持有所寄生的外部类的特性,利用内部类来完成了回调的功能,这种方式在很多代码中都被使用,比如线程,比如集合的比较器。
作者 陈字文(热衷于PM\ORACLE\J***A等,欢迎同行交流):ziwen#163.com 扣扣:4零9零2零1零零
public class Animal { private int weight; public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public class WeightAdjust{ void adjust(int weight){setWeight(weight);} } public WeightAdjust getWeightAdjust(){ return new WeightAdjust(); } public Animal(int weight) { this.weight = weight; } }
下面是回调的方法:
public static void main(String[] args) { Animal animal = new Animal(15); animal.getWeightAdjust().adjust(19); System.out.println(animal.getWeight()); }
上面的代码中,通过利用成员内部类可以持有所寄生的外部类的特性,利用内部类来完成了回调的功能,这种方式在很多代码中都被使用,比如线程,比如集合的比较器。
作者 陈字文(热衷于PM\ORACLE\J***A等,欢迎同行交流):ziwen#163.com 扣扣:4零9零2零1零零
相关文章推荐
- Java通过内部类实现回调功能
- JAVA实现通过绑定邮箱找回密码功能
- java通过jna调用科大讯飞语音云实现语音识别功能
- 通过Java实现Linux系统下grep指令的功能
- java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例
- JAVA通过正则表达式实现EXCEL小插件取文本功能
- Java通过while循环实现输入异常重新输入功能
- JAVA微信扫码支付模式二功能实现以及回调
- Java通过SMS短信平台实现发短信功能
- JAVA微信扫码支付模式二功能实现以及回调
- Java通过SMS短信平台实现发短信功能
- 转自:Java通过SMS短信平台实现发短信功能
- java通过内部类实现多线程
- Java通过SMS短信平台实现发短信功能
- java回调及匿名内部类实现
- Java通过SMS短信平台实现发短信功能 含多语言
- 需求:iOS自定义protocol与delegate来实现类似java的接口回调功能
- Java通过SMS短信平台实现发短信功能
- 微信扫码支付模式二功能实现,回调,记录充值,整个完整流程,java开发
- Java通过SMS短信平台实现发短信功能