简体   繁体   中英

Override a method in another class

I want the class MyContent.class wil be used in others projects, like a library, but it's posible to pass the code will be used inside the onChange when I create it?

MyContent.class

public class MyContent extends ContentObserver {

  @Override
  public void onChange(boolean selfChange, Uri uri) {
  }
}

Main.class :

  public class Main extends AsyncTask {
      MyContent myContent= new MyContent(new Handler());

      @Override
      public void myContent.onChange(boolean selfChange, Uri uri) {
        //posibility to use the uri parameter
        Log.d("d", "onChange fired");
      }
  }

Output: OnChange fired*

You can create an anonymous class which inherits from MyContent and overrides the method:

public class Main extends AsyncTask {
    MyContent myContent = new MyContent(new Handler()) {
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            //posibility to use the uri parameter
            Log.d("d", "onChange fired");
        }
    };
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM