Java---匿名类

今天学习Android开发,为实现button控件的onClick事件,其中一种实现方法是用匿名类,在此把Java中匿名类进行总结。

匿名内部类--没有名字的内部类,因为没有名字,所以只能使用一次

匿名内部类--它通常用来简化代码编写

使用匿名内部类前提条件:必须继承一个父类或实现一个接口

注:只要一个类是抽象的或是一个接口,那么其子类中的方法都可以使用匿名内部类来实现;

最常用的情况就是在多线程的实现上,因为要实现多线程必须继承Thread类或是继承Runnable接口

1:未使用匿名类实现方式举例

 1 abstract class Person{
 2     abstract void eat();
 3 }
 4 
 5 class Student extends Person{
 6     void eat(){
 7         System.out.println("Student eat!");
 8     }
 9 }
10 public class InnerClass {
11     public static void main(String[] args){
12         Person stu = new Student();
13         stu.eat();
14     }
15 
16 }

输出结果:>Student eat!

实现过程:Student类继承了Person类,作为其子类实现了一个实例,并将其向上转化为Person类的引用。

  2:匿名内部类基本实现方式

 1 abstract class Person{
 2     public abstract void eat();
 3 }
 4 
 5 public class InnerClass {
 6     public static void main(String[] args){
 7         Person stu = new Person(){
 8             public void eat(){
 9                 System.out.println("Student eat!");
10             }
11         };
12         stu.eat();
13     }
14 
15 }

输出结果:>Student eat!

实现过程:将Person类中的抽象方法eat()直接放在了大括号中实现,注意大括号后要有一个分号!

3:匿名内部类用在接口上

 1 interface Person{
 2     public abstract void eat();
 3 }
 4 
 5 public class InnerClass {
 6     public static void main(String[] args){
 7         Person stu = new Person(){
 8             public void eat(){
 9                 System.out.println("Student eat!");
10             }
11         };
12         stu.eat();
13     }
14 
15 }

输出结果:>Student eat!

4:线程Thread类中匿名类的实现

(1)Thread类中run()方法实现

 1 public class InnerClass {
 2     public static void main(String[] args){
 3         Thread t = new Thread(){
 4             public void run(){
 5                 for(int i = 0; i < 5; i++){
 6                     System.out.println("x = " + i +";");
 7                 }
 8             }
 9         };
10         t.start();
11     }
12 
13 }

输出结果:

x = 0;
x = 1;
x = 2;
x = 3;
x = 4;

(2)使用Runnable接口进行实现

 1 public class InnerClass {
 2     public static void main(String[] args){
 3         Runnable r = new Runnable(){
 4             public void run(){
 5                 for(int i = 0; i < 5; i++){
 6                     System.out.println("x = " + i +";");
 7                 }
 8             }
 9         };
10         Thread t = new Thread(r);
11         t.run();
12     }
13 
14 }

运行结果和上面一样,通过Runnable的实例r,将Thread和Runnable联系在一起。

5:扩展:在Android开发中button实现OnClickListener()事件使用匿名内部类实现主要代码如下:

 1 /*
 2          * 初始化Button控件;
 3          * 设置Button的监听器,通过监听器实现点击操作的事情
 4          */
 5         loginButton = (Button)findViewById(R.id.button1);
 6         loginButton.setOnClickListener( new OnClickListener(){
 7             public void onClick(View args0){
 8                 System.out.println("欢迎您!");
 9             }
10         });

注意,整个OnClickListener()事件的实现均包含在setOnClickListener()方法中

一种andriod平台中的调试方法:

可以使用“Log”函数,来达到自己在程序码中加入一个个“记录点”或“检查点”,并可以通过开发环境中的“LogCat”工具来查看记录。当程序流程每次运行到“记录点”时,相应的“记录点”就会在开发工具记录中输出一条查错用的信息。开发者通过分析这些记录,来检查程序执行的过程、使用到的参数,是否与我们期望的结果符合。依此来判断程序码中可能出错的区域,以便对症根治造成问题的程序段。

android.util.Log常用的方法有以下5个:Log.v(),Log.d(),Log.i(),Log.w()以及Log.e()。根据首字母对应VERBOSE,DEBUG,INFO,WARN,ERROR。

 

颜色

消息

说明

Log.v

黑色

任何消息verbose

包括以下所有消息

Log.d

蓝色

调试信息debug

Log.i

绿色

提示信息information

不会输出Log.v和Log.d的信息,但会显示i、w和e的信息

Log.w

橙色

警告warning

注意优化代码,还会输出Log.e的消息

Log.e

红色

错误error

需要根据它查看栈的信息,并认真的分析错误

以上log按优先级从低到高的顺序至上而下排列的,换句话说,选择某一种输出类型后,表格中在它下面的类型也会输出。

方法:

第一步:打开LogCat视窗

 

Eclipse菜单 -> Windows -> Show View ->Other -> Android -> LogCat,在控制台窗口出现LogCat视窗。

第二步:在工程中实验

界面文件中添加一个Button按钮,用于点击按钮出现Log日志信息。

 1         /*
 2          * 初始化Button控件;
 3          * 设置Button的监听器,通过监听器实现点击操作的事情
 4          */
 5         loginButton = (Button)findViewById(R.id.button1);
 6         loginButton.setOnClickListener( listener);
 7         
 8         
 9     }
10     OnClickListener listener = new OnClickListener(){
11     public void onClick(View args0){
12         //System.out.println("欢迎您!");
13         Log.i("tag", "欢迎亲~~~");
14     }
15     };

运行结果图片:

另外,OnClickListener()事件也可以通过独立类进行实现等

原文地址:https://www.cnblogs.com/graceting/p/4041531.html