有没有办法在java中调用父类中的函数?

时间:2011-09-10 00:12:51

标签: java android

我正在为我的机器人屏幕制作基类,它有一个列表控件。是否有一种方法可以调用基类的onListItemClick,表示已选择列表项。它可以调用父类中的函数??

代码:

public class cHome extends cBase {
String[] MyItems={ 
        "Gate Directions",
        "Food & Beverges",
        "Shjops",
        "Banking",
        "Official Agencies",
        "Amenities & Services",
        "Restrooms"
        };

public void onCreate(Bundle icicle) {
    super.onCreate(icicle, MyItems);
//  Display=MyItems;
}

代码:

public class cBase extends ListActivity  {

String[] items={"CASUAL","DRESSES","CAREER","OUTWEAR","FOOTWEAR",
        "JEWELRY","ACCESSORIES"};
String[] Display;


public void onCreate(Bundle icicle, String[] items2) {
     Display=items2;
    super.onCreate(icicle);

    setContentView(R.layout.baselayout);
    setListAdapter(new IconicAdapter(this));
//  selection=(TextView)findViewById(R.id.selection);

// set values in header
     // set header
     TextView mFlight = (TextView)findViewById(R.id.idFlyerFlightNumber);
     mFlight.setText( cGlobals.mFlightNumber);     

     TextView mDes = (TextView)findViewById(R.id.idFlyerDestanation);
     mDes.setText( cGlobals.mDestanation);

}


class IconicAdapter extends ArrayAdapter {
    Activity context;

    IconicAdapter(Activity context) {
        super(context, R.layout.row, Display);

        this.context=context;
    }

    public View getView(int position, View convertView,
                                            ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View row=inflater.inflate(R.layout.row, null);
        TextView label=(TextView)row.findViewById(R.id.label);

        label.setText(Display[position]);
        ImageView icon=(ImageView)row.findViewById(R.id.icon);

//      String[] items={"CASUAL","DR ESSES","CAREER","OUTWEAR","FOOTWEAR",
    //          "JEWELRY","ACCESSORIES"};

        switch(position)
        {
        case 0:
    //      icon.setImageResource(R.drawable.formobile_items);
            break;

        case 1:
        //  icon.setImageResource(R.drawable.fashion_dress);
            break;

        case 2:
//          icon.setImageResource(R.drawable.fashion_career);
            break;

        case 3:
//          icon.setImageResource(R.drawable.fashion_outwear);
            break;

        case 4:
    //      icon.setImageResource(R.drawable.fashion_footwear);
            break;

        case 5:
//          icon.setImageResource(R.drawable.fashion_jewelry);
            break;

        case 6:
//          icon.setImageResource(R.drawable.fashion_accessories);
            break;


        }
        return(row);
    }
}
}

2 个答案:

答案 0 :(得分:5)

如果我理解你的问题,你想要的是让基类在扩展它的类上调用方法。

实现这一目标的一种方法是建立一个“合同”,规定扩展类必须实现哪些方法。您可以使用抽象类或接口来实现。然后,基类知道当类扩展它时可以使用哪些方法,并且可以进行调用。

public abstract class BaseThing {

   public void foo() {
       bar();
   }

   public abstract bar();
}

然后:

public ParentThing extends BaseThing {
    public void bar() {
        System.out.println( "Hey, look at that.  It worked!" );
    }
}

答案 1 :(得分:3)

您可以覆盖子类中的onListItemClick,只需在其上调用super.onListItemClick即可。或者不要覆盖它,当选择列表中的项目时,将自动调用基类的onListItemClick方法。