在子类的底部,我需要使用 switch 语句打印父类(super class)中的 3 行。它不会打印 System.out.println(super.display());
因为它显然是 void
。
我似乎不知道该怎么做。
super 类:
public void display()
{
String fullLocation = "";
switch (location)
{
case 'N': case 'n': fullLocation = "North London";
break;
case 'S': case 's': fullLocation = "South London";
break;
case 'E': case 'e': fullLocation = "East London";
break;
case 'W': case 'w': fullLocation = "West London";
break;
default: fullLocation = "Central London";
break;
}
System.out.println("The address of this property is at " + address + ".");
System.out.println("This property is in " + fullLocation);
System.out.println("This property has " + bedrooms + " bedrooms");
}
子类:
public void display()
{
String buyer = purchaser;
if(sold == true){
*System.out.print(super); ???*
System.out.println("The price of this property is £" + price + ".");
System.out.println("The owner of this property is " + purchaser + ".");
}
else{
System.out.println("The startic price for this property is £" + price + ".");
System.out.println("The property is still on the Market");
}
}
最佳答案
仅编写 super
不会调用该方法的父类(super class)版本。要调用父类(super class)方法,您可以将关键字 super
视为父类(super class)的实例,并使用点表示法调用该方法:
super.display();
您可以从该行中删除 System.out.println
,因为父类(super class)方法有自己的打印语句。
关于java - 尝试在父类(super class)的子类中打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27387490/