anonymous-class 一些事情

https://stackoverflow.com/questions/16785922/creating-the-instance-of-abstract-class-or-anonymous-class

https://stackoverflow.com/questions/4579305/can-we-instantiate-an-abstract-class

You can't directly instantiate an abstract class, but you can create an anonymous class when there is no concrete class:

AsyncTask.java
import java.io.*;
public abstract class AsyncTask {
    protected abstract boolean doInBackground(String params);

    public void asyncTaskPrint() {
         System.out.println("AsyncTask");
    };
}
Test.java
import java.io.*;
public class Test{
    public static void main(String [] args){
        System.out.println("hello");
        AsyncTask asyncTask = new AsyncTask() {
            public void printSomethingOther() {
                System.out.println("other");
            }
            @Override
            protected boolean doInBackground(String params) {
               System.out.println("world");
               printSomethingOther();
                return false;
            }
        };
        asyncTask.doInBackground("");
        asyncTask.asyncTaskPrint();

    }
}

猜你喜欢

转载自www.cnblogs.com/hixin/p/9298729.html