java - 尝试在父类(super class)的子类中打印

标签 java bluej

在子类的底部,我需要使用 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/

相关文章:

java - 在主类中调用/测试排序方法

java - 从另一个 Java/BlueJ 类更改 JTextField

使用引用时 BlueJ 中的 Java checkstyle 错误

java - 如何从另一个类调用方法函数?

java - 如何使用 Java8 流在下面的数组列表中找到第二高的薪水

java - 使用 iText 替换 PDF 中的字体 (Java)

java - 我可以使用 Hibernate 连接不同的数据库并从表中导入数据吗?没有预定义的对象类

java - 如何编译基于 Maven 的 Java 项目以从命令行运行它

java - eclipse:未使用的私有(private)方法没有警告

Java BlueJ 冒泡排序