这个练习题我最后运行报错求指教哪里有问题为什么
来源:3-3 自由编程
给对方把对方
2019-01-31 17:33:42
public class Food {
private String name;
private String taste;
private String kind;
@Override
public void toString(){
System.out.println(name+"属于"+kind+","+"口味"+taste);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTaste() {
return taste;
}
public void setTaste(String taste) {
this.taste = taste;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
}
<bean id="food" class="com.springtest.Food">
<property name="name" value="苹果"/>
<property name="kind" value="水果"/>
<property name="taste" value="甜甜的"/>
</bean>
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo {
@Test
public void demo1(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
Food f=(Food) applicationContext.getBean("food");
f.toString();
}
}1回答
1、关于Food的报错:
同学方法上边写的注解@Override ,写了这个注解后,它就会去帮你校验是不是重写的父类的方法,如果不是,它就会报错。
重写的父类方法要和父类中该方法的返回值,方法名,参数一致,
父类中的toSring方法是有返回值的。如果要用重写的这个方法就必须添加上返回值。
或者同学就是不想要返回值的话就去掉注解,当做子类中特有的方法,而不是重写的父类的方法了。

同学还有其他地方报错吗?
如果有,请提供一下报错信息和截图,注意不要贴在回复中,容易失去格式。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题