class 项中的构造函数项不能应用于给定类型;
Constructor Item in class Item cannot be applied to given types;
所以我对 Java 很陌生...已经使用了大约 4 周...要温柔。
我试图让我的 takeItem 方法(如下)将 itemName 变量传回我的玩家 Class 这样我就可以从我当前的房间向我的玩家添加一个项目。我收到编译器错误:class 项中的构造函数项无法应用于给定类型..
我的最终目标是让玩家 class 在将物体从房间中取出后抓住物体。
获取项目方法:
private void takeItem(Command command)
{
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
System.out.println("Take what?");
System.out.println();
return;
}
String itemName = command.getSecondWord();
Item theItem;
// Try to take an item.
theItem = new Item(player.getCurrentRoom().removeItem(itemName));
if (theItem == null)
{
System.out.println("There is no item!");
}
else
{
player.addItem(theItem);
player.getItemsCarried();//print item info
}
玩家Class:
//above code omitted//
public void setCurrentRoom(Room room)
{
currentRoom = room;
}
public Room getCurrentRoom()
{
return currentRoom;
}
//code below omitted//
public void addItem (Item thingy)
{
items.put(thingy.getName(), thingy);
}
//code below omitted//
项目 Class:
public class Item
{
// instance variables - replace the example below with your own
private String name;
private String description;
private int weight;
/**
* Constructor for objects of class Item
*/
public Item(String n, String d, int w)
{
name = n;
description = d;
weight = w;
}
//code below omitted//
房间 Class:
public class Room
{
private String description;
private HashMap <String, Room> exits;
private HashMap <String, Item> items;
//some code below omitted//
public Room (String description)
{
this.description = description;
exits = new HashMap<>();
items = new HashMap<>();
}
public void addItem (Item thingy)
{
items.put(thingy.getName(), thingy);
}
public String removeItem(String thingy)
{
items.remove(thingy);
return thingy;
}
//code below omitted
Item
class 中的构造函数采用两个 String
参数和一个 int
参数,但您正试图通过传递来创建一个新的 Item
只有一个 String
(无论 removeItem()
方法返回什么)。您可以更改 removeItem()
方法,使其 returns 被删除的 Item
,在这种情况下,您应该更改
theItem = new Item(player.getCurrentRoom().removeItem(itemName));
到
theItem = player.getCurrentRoom().removeItem(itemName);
或者您可以使用必要的参数创建一个新的 Item
。
所以我对 Java 很陌生...已经使用了大约 4 周...要温柔。
我试图让我的 takeItem 方法(如下)将 itemName 变量传回我的玩家 Class 这样我就可以从我当前的房间向我的玩家添加一个项目。我收到编译器错误:class 项中的构造函数项无法应用于给定类型..
我的最终目标是让玩家 class 在将物体从房间中取出后抓住物体。
获取项目方法:
private void takeItem(Command command)
{
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
System.out.println("Take what?");
System.out.println();
return;
}
String itemName = command.getSecondWord();
Item theItem;
// Try to take an item.
theItem = new Item(player.getCurrentRoom().removeItem(itemName));
if (theItem == null)
{
System.out.println("There is no item!");
}
else
{
player.addItem(theItem);
player.getItemsCarried();//print item info
}
玩家Class:
//above code omitted//
public void setCurrentRoom(Room room)
{
currentRoom = room;
}
public Room getCurrentRoom()
{
return currentRoom;
}
//code below omitted//
public void addItem (Item thingy)
{
items.put(thingy.getName(), thingy);
}
//code below omitted//
项目 Class:
public class Item
{
// instance variables - replace the example below with your own
private String name;
private String description;
private int weight;
/**
* Constructor for objects of class Item
*/
public Item(String n, String d, int w)
{
name = n;
description = d;
weight = w;
}
//code below omitted//
房间 Class:
public class Room
{
private String description;
private HashMap <String, Room> exits;
private HashMap <String, Item> items;
//some code below omitted//
public Room (String description)
{
this.description = description;
exits = new HashMap<>();
items = new HashMap<>();
}
public void addItem (Item thingy)
{
items.put(thingy.getName(), thingy);
}
public String removeItem(String thingy)
{
items.remove(thingy);
return thingy;
}
//code below omitted
Item
class 中的构造函数采用两个 String
参数和一个 int
参数,但您正试图通过传递来创建一个新的 Item
只有一个 String
(无论 removeItem()
方法返回什么)。您可以更改 removeItem()
方法,使其 returns 被删除的 Item
,在这种情况下,您应该更改
theItem = new Item(player.getCurrentRoom().removeItem(itemName));
到
theItem = player.getCurrentRoom().removeItem(itemName);
或者您可以使用必要的参数创建一个新的 Item
。