java - 错误: Constructor Room in class Room cannot be applied to given types

标签 java inheritance

我对java完全陌生。我花了几个小时寻找这个问题的解决方案,但每个答案都涉及传递参数或使用 void,但在这种情况下我不会这样做。

我有两个 java 文件,一个用于 Room 类,一个用于 TourHouse 类。我正在尝试在 TourHouse 类中创建一个新房间。这是我的错误,它让我发疯,我已经尝试了我能理解的一切。预先感谢您。

HouseTour.java:15: error: constructor Room in class Room cannot be applied to given
types;
     {
     ^
required: String, String
found: no arguments
reason: actual and formal arguments differ in length

这是 Room 类,一旦我能弄清楚,总共将有 7 个房间

// Room.java

import java.util.*;

public class Room
{
    // Define Instance Variables
    private String name;
    private String description;

    // Define Constructor
    public Room(String theName, String theDescription)
    {
        name = theName;
        description = theDescription;
    }

    public String toString( )
    {
    return "The " + name + "\n" + description + "\n";
    }
}

这是 HouseTour 类

import java.util.*;

public class HouseTour extends Room
{
    // Define Variables
    public Room[ ] rooms = new Room[7];

    //Define Constructor
    public HouseTour( )
    {
    rooms[0] = new Room("Living Room", "Mayonnaise and Brill Grates, Michaelsoft");

    rooms[1] = new Room("Basement", "Hopefully no dead bodies down here...");
    }

    // this is horrible and not right 
    public String rooms( ) 
    {
        for (int i = 0; i <=7; i++)
        {
            String output = "House Rooms included in tour\n";
            String output2 = output + rooms.toString() + "\n";
            return output2;
        }
    }
}

编辑:已解决,但仍然需要帮助,因为我已经完成了 n00b,:(

    // this is horrible and not right 
    public String rooms( ) 
    {
        output = "House Rooms included in tour\n";
        for (int i = 0; i <=7; i++)
        {
            output += rooms[i]; // I can't do this but how do i?
        }
        return output.toString(); // do I do this?
    }
}

我正在做的是尝试通过转换我创建的 ruby​​ 项目来学习 java。所以在 ruby 中你可以说:

def rooms
  output = "House Rooms included in tour\n"
  @rooms.each do |r|
    output += r.to_s + "\n"
  end
  return output
end
<小时/>

编辑:仍在尝试,有什么想法吗? 添加了公共(public) String ;和公共(public)字符串输出;到声明

    // this is horrible and not right 
    public String rooms( ) 
    {
        s = ""
        output = "House Rooms included in tour\n";
        for (int i = 0; i <=7; i++)
        {
            s += rooms[i];
        }
        s.toString() // I don't know
        return output + s; // do I do this?
    }
}
<小时/>

编辑:通过充满鳗鱼的气垫船解决了

最佳答案

啊,我明白你的问题:HouseTour 扩展了 Room。不要这样做! HouseTour 不是 Room 类型的更具体情况,因此不应扩展此类。它不满足“is-a”规则,类似于尝试将 Bus 定义为 SchoolKid 的子类。就像巴士不是 SchoolKid 的一种类型而是包含 SchoolKids 一样,HouseTour 也不是房间而是包含房间。它满足has-a关系,而不是is-a关系。

如果在这种情况下继承是正确的,您的 HouseTour 构造函数将需要调用 Room super 构造函数并传入两个字符串参数:

// Don't do this!!!
public class HouseTour extends Room {

    public HouseTour() {
      super("foo", "bar");
      ....
    }

但是话虽如此,继承在这里也不合适——只需摆脱extends Room,你就可以自由了。

例如,

public class HouseTour {  // no extends!
  private Room[] rooms; // has-a not is-a

  public HouseTour() {
    // don't call super here
  }

另外,根据我的评论,这会给你丑陋的输出:rooms.toString()

而是迭代数组并从数组中的每个 Room 项目获取 toString() 结果。

<小时/>

编辑
关于 rooms() 方法的建议:

  • 在循环之前创建一个 String 或 StringBuilder。
  • 在循环内构建 String 或 StringBuilder。
  • 循环之后返回 String 或 StringBuilder#toString。
  • 在循环内部从列表中当前的 Room 项目获取 toString()。
  • 在调用 rooms[i] 项的方法之前,您需要检查它是否不为 null。
<小时/>

编辑2

您声明:

public String rooms( ) 
{
    output = "House Rooms included in tour\n";
    for (int i = 0; i <=7; i++)
    {
        output += rooms[i]; // I can't do this but how do i?
    }
    return output.toString(); // do I do this?
}

导致问题,但您没有指定问题。

我自己,我会做这样的事情:

public String rooms( ) {
    // declare your String locally, not globally in the class
    String output = "House Rooms included in tour\n";
    // again, avoid using "magic" numbers like 7
    for (int i = 0; i < rooms.length; i++) {
        output += rooms[i].toString(); // **** you must extract Room's String
    }
    return output;  // no need to call toString() on a String
}

关于java - 错误: Constructor Room in class Room cannot be applied to given types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20111171/

相关文章:

python - 除了 (MyClass, self) 之外,您还会将其他任何内容传递给 super() 吗?

java - 重用继承来定义Java中的方法

java - 实现子类的好方法

java - Facebook和Google认证如何实现单点退出功能

java - InputStream.available() 并从 oracle 中完整读取文件注释

URL 的 Java Android 重定向不起作用

java - 如何遍历列以在 Excel 中获取行标记数据?

java - XSSFSheet 无法转换为 java.io.File

.net - 为什么基类的成员与派生类中的相同成员不同?

inheritance - 如何从WCF中未标记DataContractAttribute或SerializableAttribute的类继承?