interface Alarm{
public String getBrand();
public void larmNotice();
}
public class Bentley extends Car {
public String getInfo(){
return "Bentley";
}
public String getBrand(){
return "宾利牌汽车专用报警器";
}
public void larmNotice(){
System.out.println("主人,有人在盗窃您的宾利车!");
}
}
public class Customer {
public static void main(String args[]){
Bentley b = new Bentley();
System.out.println("顾客要购买宾利:");
//Car bentley=CarFactory.getCar("Bentley");
System.out.println("提取汽车:"+b.getInfo());
//System.out.println("提取汽车:"+bentley.getInfo());
// System.out.println("提取汽车:"+b.getInfo());
System.out.println("赠送汽车报警器:"+b.getBrand());
System.out.println(b.larmNotice()); //此处出错?
}
}
你的larmnotise 方法就是用来打印的,拿出来单独调用就行了
类没有声明接口,应为:
public class Bentley extends Car implements Alarm{
.......
}
哦,b.larmNotice是void,直接打印
System.out.println()参数值应该是字符串,你用的却是void方法larmNotice(),因此没办法打印。
可将
public String larmNotice(){
String str= "主人,有人在盗窃您的宾利车!";
return str;
}
b.larmNotice()无返回值,所以不能打印,直接将System.out.println(b.larmNotice());改为b.larmNotice();或者在larmNotice方法内设置一个返回值。
直接将System.out.println(b.larmNotice());改为b.larmNotice()
public void larmNotices()是个无返回值方法,不能输出
void 你让打印什么?
该方法没有返回值,System.out.println()接受了一个无效参数,报错很正常。。。。
可以通过查看PrintStream的源码可知:println()这个方法的调用执行过程是:如果()里面有获得对象,则对应输出该对象,
比如char,boolean,int,long,String等。你写的方法返回值是void,说明对象为空。对象为空的源码是:
public void println() {
newLine();
}
所有,此时的println功能就是换行而已了。还有就是,我在idea15里面试写了一下,在make前,会直接提示错误:cannot resolve method
你的larmNotice()方法返回的是一个void,所以不能够通过System.out.println()打印,可以将 larmNotice()改写,设置一个返回值,比如这样:
public String larmNotice(){
return"主人,有人在盗窃您的宾利车!";
}
打印参数不能是void,直接调用方法就行啦
这就尴尬了……打印void....
System.out.println()参数值应该是字符串,你用的却是void方法larmNotice(),因此没办法打印。