怎么把Food对象封装到Person中?

怎么把Food对象封装到Person中?

	/**
	 * showPic显示图片
	 */
	private void showPic(int count) {
		// ((Food)(lis_get.get(count))).getSrc()
		imageView.setImageResource(((Food) (lis_get.get(count))).getSrc());
	}

	/**
	 * 为ToggleButton添加监听对象
	 */
	class ToggleButtonListener implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			showPic(count);

			if (toggleButton.isChecked()) {

				Person person = new Person(editText.getText().toString(), sex,
						((Food) (lis_get.get(count))));//报错了

				Toast.makeText(MainActivity.this, "", 100).show();
			} else {

				if (count != (lis_get.size() - 1)) {
					count++;
				} else {
					Toast.makeText(MainActivity.this, "已经到最后", 100).show();
				}
			}

		}

	}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

4回答
提问者 瞳劫 2017-02-25 16:09:02
	Person person = new Person();
				person.setSex(sex);
				person.setName(editText.getText().toString());
				person.setFood(((Food) (lis_get.get(count))));


提问者 瞳劫 2017-02-25 16:08:11
public class Person {
      private String name;
      private String sex;
      Food food;
      
      
      public Person(){
    	  
      }
      
	public Person(String name, String sex, Food food) {
		super();
		this.name = name;
		this.sex = sex;
		this.food = food;
	}
	
	



	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Food getFood() {
		return food;
	}
	public void setFood(Food food) {
		this.food = food;
	}
      
}


没有马甲线的安琪拉 2017-02-25 16:00:34

这个报错指的是你的Person的构造方法里没有支持Food类型的参数,如果只是想把Food放到实例化过程中,需要在Food里面添加对应的构造方法。

  • 提问者 瞳劫 #1
    有啊O_o public class Person { private String name; private String sex; Food food; public Person(){ } public Person(String name, String sex, Food food) { super(); this.name = name; this.sex = sex; this.food = food; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Food getFood() { return food; } public void setFood(Food food) { this.food = food; } }
    2017-02-25 16:07:47
  • 提问者 瞳劫 #2
    后来我改成一个一个加,就好了
    2017-02-25 16:08:49
提问者 瞳劫 2017-02-25 15:10:50
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师