java– 如果发现条件为真,如何覆盖方法?

java– 如果发现条件为真,如何覆盖方法?,第1张

概述在我的MainActivity类中,如果在我的视图中按下按钮,我想停止覆盖attachBaseContext方法.情况如下:publicclassMainActivityextendsAppCompatActivity{booleanvalue=true;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.on

在我的MainActivity类中,如果在我的视图中按下按钮,我想停止覆盖attachBaseContext方法.

情况如下:

public class MainActivity extends AppCompatActivity {    boolean value = true;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //setting content vIEw and stuff    }    //the following should be overrIDden only if value == true.     //I can change the value to false by clicking a button in my vIEw.    @OverrIDe    protected voID attachBaseContext(Context base) {        super.attachBaseContext(ZeTarget.attachBaseContext(base,this));    }    public voID stopOverrIDing (VIEw vIEw) {        value = false;    }

在我看来,我的主活动布局中有一个按钮,在单击时调用stopOverrIDing()方法:

<buttonandroID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:ID="@+ID/button"androID:onclick="stopOverrIDing"androID:text="@string/change"/>

我在所有活动中都有相同的覆盖attachBaseContext()方法.我的问题是,是否有可能在单击主活动中的按钮后,我可以在所有活动中停止覆盖此attachBaseContext()方法?

解决方法:

你不能做出关于方法是否被重写的运行时决定(没有动态生成类,这很复杂[我不知道Dalvik是否支持它]).

只需检查方法中的条件

protected voID attachBaseContext(Context base) {    if (this.value) {        // Your special behavior        super.attachBaseContext(ZeTarget.attachBaseContext(base,this));    } else {        // The super's behavior, as though you hadn't overrIDden the method        super.attachBaseContext(base);    }}
总结

以上是内存溢出为你收集整理的java – 如果发现条件为真,如何覆盖方法?全部内容,希望文章能够帮你解决java – 如果发现条件为真,如何覆盖方法?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1115025.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存