有人可以解释这个Java code [英] Can Somebody Explain this java code

查看:288
本文介绍了有人可以解释这个Java code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始在Android和我的Java是柠生锈。我不记得曾经看到嵌套在之前像这样的其他功能的功能。可能有人向我解释到底是什么最终没有,并解释为什么你要嵌套在另一个这样的功能?

 私人最终处理程序处理程序=新的处理程序(){
        @覆盖
        公共无效的handleMessage(最终报文MSG){
            Log.v(Constants.LOGTAG,+ ReviewList.CLASSTAG +工作线程完成,设置ReviewAdapter);
            progressDialog.dismiss();
            如果((评论== NULL)||(reviews.size()== 0)){
                empty.setText(无数据);
            } 其他 {
                reviewAdapter =新ReviewAdapter(ReviewList.this,评论);
                setListAdapter(reviewAdapter);
            }
        }
    };
 

解决方案
  • 这是一个匿名类。 什么是实际发生的是, 子类处理程序正在 与被覆盖的创建 的handleMessage 功能。

      

    一个关于最优雅的事情   匿名类是他们让   你完全定义一次性类   在需要的地方。此外,   匿名类有一个简洁的   语法,减少凌乱,   code。

  • 您还问有人能 给我解释一下到底是什么最后 没有一个很好的解释可 这里发现。

    在您的例子的情况下,最后关键字阻止任何人从能够分配一个新的实例/变量处理的空实例意味着我不能写行处理程序= 空; 处理程序=新的处理程序(){ ...} 后,你的榜样code段。

I'm just starting out on android and my java is verry rusty. I can't remember ever seeing a function nested in another function like this before. Could somebody explain to me exactly what final does and explain why you would nest a function in another like this?

private final Handler handler = new Handler() {
        @Override
        public void handleMessage(final Message msg) {
            Log.v(Constants.LOGTAG, " " + ReviewList.CLASSTAG + " worker thread done, setup ReviewAdapter");
            progressDialog.dismiss();
            if ((reviews == null) || (reviews.size() == 0)) {
                empty.setText("No Data");
            } else {
                reviewAdapter = new ReviewAdapter(ReviewList.this, reviews);
                setListAdapter(reviewAdapter);
            }
        }
    };   

解决方案

  • This is an Anonymous Class. What is actually happening is that a subclass of Handler is being created with an overridden handleMessage function.

    One of the most elegant things about anonymous classes is that they allow you to define a one-shot class exactly where it is needed. In addition, anonymous classes have a succinct syntax that reduces clutter in your code.

  • You also asked "Could somebody explain to me exactly what final does". A nice explanation can be found here.

    In the case of your example the final keyword stops anybody from being able to assign a new instance / null the instance of the variable "handler" meaning I cannot write the line handler = null; or handler = new Handler() { ... } after your example code snippet.

这篇关于有人可以解释这个Java code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆