我是java的新手。我已经搜索了几个小时来解决这个问题,但每个答案都涉及传递args或使用我在这种情况下不做的空白。
我有两个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。所以在红宝石中你说:
def rooms
output = "House Rooms included in tour\n"
@rooms.each do |r|
output += r.to_s + "\n"
end
return output
end
编辑:还在尝试,有什么想法吗? 添加了公共String;和公共字符串输出;声明
// 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?
}
}
编辑:感谢Hovercraft Full Of Eels
答案 0 :(得分:1)
啊,我看到你的问题:HouseTour扩展了房间。不要这样做! HouseTour不是Room类型的更具体的情况,因此不应扩展此类。它不符合“is-a”规则,并且类似于尝试将Bus定义为SchoolKid的子类。就像公共汽车不是一种SchoolKid而是包含SchoolKids一样,HouseTour不是房间而是包含房间。它符合 has-a 关系,而非 is-a 关系。
如果继承在这种情况下是正确的,那么你的HouseTour构造函数需要调用Room超级构造函数并传入两个String参数:
// 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()结果。
修改强>
关于你的房间的建议()方法:
修改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
}