试图从超类中打印一个子类

时间:2014-12-09 19:45:35

标签: java bluej

在子类的底部,我需要使用switch语句从我的超类中删除3行。它不会打印System.out.println(super.display());,因为它显然是void

我似乎无法弄明白该怎么做。

超级班:

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");
   }
}

2 个答案:

答案 0 :(得分:1)

只是编码super并不会调用该方法的超类版本。要调用超类方法,可以将关键字super视为超类的实例,并使用点表示法调用该方法:

super.display();

您可以从该行中删除System.out.println,因为超类方法有自己的打印语句。

答案 1 :(得分:0)

只需撰写super.display(),就不需要System.out.println