013、匿名对象
来源:互联网 发布:python class 编辑:程序博客网 时间:2024/06/11 13:06
一、匿名对象:就是没有名字的对象。二、匿名对象的应用场景:A:调用方法,仅仅只调用一次的时候。注意:调用多次的时候,不适合。那么,这种匿名调用有什么好处吗?有,匿名对象调用完毕就是垃圾。可以被垃圾回收器回收。B:匿名对象可以作为实际参数传递三、案例class Student {public void show() {System.out.println("我爱学习");}}class StudentDemo {public void method(Student s) {s.show();}}class NoNameDemo {public static void main(String[] args) {//带名字的调用Student s = new Student();s.show();s.show();System.out.println("--------------");//匿名对象//new Student();//匿名对象调用方法new Student().show();new Student().show(); //这里其实是重新创建了一个新的对象System.out.println("--------------");//匿名对象作为实际参数传递StudentDemo sd = new StudentDemo();//Student ss = new Student();//sd.method(ss); //这里的s是一个实际参数//匿名对象sd.method(new Student());//在来一个new StudentDemo().method(new Student()); }}
阅读全文
0 0
- 013、匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- 匿名对象
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- 2018科大讯飞校招笔试题
- QT之使用QSemaphore信号量同步线程小例子
- Android安全知识库
- SwipeRefreshLayout加入加载更多
- 013、匿名对象
- 014、面向对象的三大基本特征-封装
- cron表达式
- Servlet 笔记二
- eclipse中使用git
- OpenGL的SetupRC()函数解析
- 015、this关键字
- mongoDB简介
- 绿盟前端笔试题