1

我有 A 类,它配置自己解析 xml 文件。B 类扩展 A 并配置自己解析一个 xml 文件,该文件具有比前一个更多的标签。

class A {
  protected String mFoo;

  protected loadFromXml (...){
     ...
     mFoo = ...
     ...
  }
}

class B extends A {
    protected String mBar;

    protected loadFromXml (...){
    super.loadFromXml(...);

    ...
    mBar = ...
    ...
    }
}

现在,我有 ActivityA,它通过 A 类进行自我配置:

class ActivityA extends Activity {

protected A mAInstance;

    public void onCreate(Bundle savedInstanceState) {
        ...
        mAInstance.loadFromXml(...);
    ...
    other code...
        ...
    }
}

和活动B:

class ActivityB extends ActivityA{

   protected B mBInstance;

   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    ...
    mBInstance.loadFromXml(...);
...
   }
}

现在,问题是:ActivityB 拥有 mAInstance 和 mBInstance,它们共享相同数据 (mFoo) 的子集。如何仅加载 mBInstance?我当然需要在ActivityB的onCreate中调用super ...

4

3 回答 3

1

首先,我假设 ActivityB 扩展了 ActivityA,否则无法从 ActivityB 访问 A。

然后,您可以从 B 中删除 mBInstance,因为您已经有一种可用的 A。加载 xml 时,将对 loadFromXML 的调用移至新方法,例如:

在活动 A 中:

protected onCreate(...) {
 ....
 loadXML();
}

protected void loadXML() {
  getNewInstance().loadFromXML();
}

protected A getNewInstance() {
  return new A();
}

然后在 ActivityB 中,您只需要覆盖 getNewInstance() 即可:

protected A getNewInstance() {
 return new B();
}

mBInstance.loadFromXml(...);ActivityB#onCreate(...) 多态性中删除以进行救援!

于 2013-05-30T08:37:38.950 回答
0

I would use polymorphism and move the loadFromXml() call to a protected method, as suggested by gunar. You also need to solve how to access the instance of A or B with correct types, though. Here is an example:

Classes A and B:

class A {
   public String mFoo;
   protected void loadFromXml() {}
}

class B extends A {
    public String mBar;

    @Override
    protected void loadFromXml() {}
 }

Class ActivityA

class ActivityA {
    private A mAInstance; // do not access this directly, use getter

    protected A getMInstance() { // getter
        return mAInstance;
    }

    @override
    public void onCreate() {
        initMInstance();
    }

    protected void initMInstance() {
        mAInstance = new A();
        mAInstance.loadFromXml();
    }

    private void f() {
        String s = getMInstance().mFoo; // access mAInstance like this
    }
}

Class ActivityB

class ActivityB extends ActivityA {
    private B mBInstance;

    @Override
    protected B getMInstance() { // override getter, ActivityA will see type A, activityB type B
        return mBInstance;
    }

    @Override
    public void onCreate() {
        initMInstance();
    }

    @Override
    protected void initMInstance() {
        mBInstance = new B();
        mBInstance.loadFromXml();
    }

    private void f() {
        String s1 = getMInstance().mFoo; // here you can access both properties
        String s2 = getMInstance().mBar;
    }
}
于 2013-05-30T08:59:33.523 回答
-1

B.loadFromXml()你在做

super.loadFromXml(...);

您根本不会影响 mAinstance。由于它不是静态的,我假设您正在更新其中的this属性而不是创建任何新实例;B即使它是类中的代码,您也将更新实例A

于 2013-05-30T08:29:07.450 回答