简体   繁体   English

从抽象父级访问子类的引用

[英]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. 在私有类字段中的一种方法中,无论子类扩展了抽象类,我都需要引用this方法。

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 . 在ActivityCrazy不是抽象类之前,我可以使用ActivitySpecific.this But now I realized that many classes need the same private FoolClickedButton, so I created this abstract class. 但是现在我意识到许多类都需要相同的私有FoolClickedButton,因此我创建了这个抽象类。 The problem is that FoolClickedButton's onClick method must refer to itself. 问题在于FoolClickedButton的onClick方法必须引用自身。

Before ActivityCrazy was not an abstract class, so I could use ActivitySpecific.this. 在ActivityCrazy不是抽象类之前,我可以使用ActivitySpecific.this。 But now I realized that many classes need the same private FoolClickedButton, so I created this abstract class. 但是现在我意识到许多类都需要相同的私有FoolClickedButton,因此我创建了这个抽象类。 The problem is that FoolClickedButton's onClick method must refer to itself. 问题在于FoolClickedButton的onClick方法必须引用自身。

Have you tried just using ActivityCrazy.this ? 您是否尝试过仅使用ActivityCrazy.this

It should work, and reference the actual instantiated concrete subclass object. 它应该工作,并引用实际的实例化具体子类对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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