简体   繁体   English

为什么java下属对象不能访问父字段?

[英]Why can't java subordinate object access parent field?

I have an object that I instantiate twice, and each one needs to affect something in the other object. 我有一个我实例化两次的对象,每个对象都需要影响另一个对象中的某些东西。 Yes, it's ugly, but in Android, I want to make a radio button in one display/tab match the state in another display/tab. 是的,它很难看,但在Android中,我想在一个显示/标签中设置一个单选按钮,与另一个显示/标签中的状态相匹配。 I suppose I could have a method in the parent that updates both instantiations of the radio button, that'd probably be a better way to do it. 我想我可以在父级中有一个方法来更新单选按钮的两个实例,这可能是一种更好的方法。 But I don't understand why what I've done below doesn't work and I'd like to understand why even if I don't use it. 但我不明白为什么我在下面所做的事情不起作用,我想理解为什么即使我不使用它。

I'll just use 'someField' for this example as the cross-access field. 我将在本例中使用'someField'作为交叉访问字段。 I pass 'this' into each instantiated object for who the parent is, then I should be able to access a field in the other 'brother' object thru the parent object I would think, but it doesn't seem to work. 我将'this'传递给父对象的每个实例化对象,然后我应该能够通过我想到的父对象访问另一个'兄弟'对象中的字段,但它似乎不起作用。 Here's a simplified version of the code (actually the two classes are in two files of course): 这是代码的简化版本(实际上这两个类当然是在两个文件中):

public class EtMain extends TabActivity  {
    public WxFields  mDepWx;
    public WxFields  mArrWx;

    public void onCreate(Bundle savedInstanceState) {
        mDepWx = new WxFields(this);
        mArrWx = new WxFields(this);
    }
}

public class WxFields {
    private Activity mParent; // Main Window object
    public int someField;

    public WxFields(Activity myParent) {
        mParent = myParent;
    }

    public void someSetup() {
        Button mButton = (Button) mParent.findViewById(R.id.someRes); //<---mParent works here
        mParent.mArrWx.someField = 0; //<------------ mArrWx cannot be resolved
        //I don't seem to be able to access a field in the parent object
        //Yes, it's not symmetrical, but the code is just for illustration purposes

    }
}

Actually, if you declare mParent as EtMain and not as Activity , it will work (though it would be ugly...) 其实,如果你申报mParentEtMain而不是Activity ,它会工作(虽然这将是丑陋的...)

Activity doesn't have such field ( mArrWx ) Activity没有这样的字段( mArrWx

public class EtMain extends TabActivity  {
    public WxFields  mDepWx;
    public WxFields  mArrWx;

    public void onCreate(Bundle savedInstanceState) {
        mDepWx = new WxFields(this);
        mArrWx = new WxFields(this);
    }
}

public class WxFields {
    private EtMain mParent; // Main Window object
    public int someField;

    public WxFields(EtMain myParent) {
        mParent = myParent;
    }

    public void someSetup() {
        Button mButton = (Button) mParent.findViewById(R.id.someRes); //<---mParent works here
        mParent.mArrWx.someField = 0;
    }
}

It looks like you'll have to cast mParent to type EtMain in order for the field to be visable. 看起来您必须将mParent为键入EtMain才能使该字段可见。
mArrWx is not a member of type Activity . mArrWx不是Activity类型的成员。

I would have guessed that you'd use a Listener implementation that would take the references of the objects that need to be kept in synch. 我猜想你会使用一个Listener实现来获取需要保持同步的对象的引用。 The action method would check to see which was updated and synch up the other. 动作方法将检查哪个更新并同步另一个。

http://developer.android.com/guide/topics/ui/ui-events.html http://developer.android.com/guide/topics/ui/ui-events.html

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

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