不好的继承

Bad Inheritance

我正在开发一个 Android 应用程序,但由于某种原因,我的继承无法正常工作。

WITBase:

public class WITBase extends Activity {
    public static List <History> searches = new ArrayList<History>();
    public int index;
    public boolean newHistory(History history){
        searches.add(history); 
        index = history.index;
        
        System.out.println(index);  //Shows the right number    
        return true;
    }

问题是:

WITInfo:

    public class WITInfo extends WITBase{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        System.out.println(super.index);//always shows 0 :/
        switch (super.index){
            case 2: case 3:
                setContentView(R.layout.mb_wit);
            case 4:case 5:case 6:
                setContentView(R.layout.bb_wit);
            case 7:
                setContentView(R.layout.it_wit);
            case 8:
                setContentView(R.layout.ftg_wit);
            case 1:case 9:
                setContentView(R.layout.tl_wit);
            default:
                setContentView(R.layout.info_wit);
        }

我不知道这些信息是否足以让你帮助我,所以如果你需要更多的东西,我会写在这里。

WITBase.index 的默认值为 0。OnCreate 是第一个被调用的东西。这样就解释了 0 值。

newHistory 设置值时,您应该在运行时检查该值。