简体   繁体   中英

Accessing the reference of a child class from an abstract parent

I have an abstract class. It contains a private class field. In one of the methods in the private class field, I need to refer to this of whatever child class extends the abstract class.

public abstract class ActivityCrazy extends Activity {
 private class FoolClickedButton implements OnClickListener {
  public void onClick(View v) {
   startActivity(
    new Intent(
     IMPLEMENTER_OF_ABSTRACT_CLASS.this,
     AnotherClass.class
   )
  }
 }
}

How do I properly do this?

Before ActivityCrazy was not an abstract class, so I could use ActivitySpecific.this . But now I realized that many classes need the same private FoolClickedButton, so I created this abstract class. The problem is that FoolClickedButton's onClick method must refer to itself.

Before ActivityCrazy was not an abstract class, so I could use ActivitySpecific.this. But now I realized that many classes need the same private FoolClickedButton, so I created this abstract class. The problem is that FoolClickedButton's onClick method must refer to itself.

Have you tried just using ActivityCrazy.this ?

It should work, and reference the actual instantiated concrete subclass object.

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