首先,让我解释一下我要做的事情。在java中,我正在创建一个与我正在制作的高中学校cca程序相关的游戏(已经很好地在vb6中进行),但我仅限于我能在其中完成的工作,因此我正在工作用java。名为Parts的类的代码如下所示:
public class Parts implements Cloneable{
public static Parts[] PartsList = new Parts[4096];
public static HashMap<Integer,Boolean> ArrayList = new HashMap<Integer,Boolean>();
protected boolean PartConstructerCalled = false;
public int partTextureIndex;
public final int partID;
protected float partStat;
protected String partConversion;
private String partName;
/**
* To get everything started.
*/
public static void Init_Parts(){}
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
/**
* Call this to create a new part.
* Game crashes if an id you give is already used.
* @param id Id to give to the part.
*/
public Parts(int id, int textureid)
{
this.PartConstructerCalled = true;
if(PartsList[id] != null){
Variables.HackLog.severe("Part id of " + id + " is already used.");
String partname = PartsList[id].getPartName();
if(partname != "Un-named"){
throw new IllegalArgumentException("id " + String.valueOf(id) + " was trying to take up part id of " + id + " which is already taken up by "+PartsList[id].getPartName() + ".");
}else{
throw new IllegalArgumentException("id " + String.valueOf(id) + " was trying to take up part id of " + id + " which is already used.");
}
}else{
this.partID = id;
this.partName = "Un-named";
this.partTextureIndex = textureid;
PartsList[id] = this;
ArrayList.put(id, false);
}
}
}
好的,这是Parts.java的一些代码,但它是显示我需要修复的主要内容。 MainParts.java是我想要发生但不是的代码(再次两个代码片段都不是完整代码):
public class MainParts extends Parts{
public static Parts[] CPU = CreatePartArray(1,1,4,"CPU");
public static final float[] CPUStats = {1.28f,2.0f,3.36f,5.0f};
public static final Parts Memory = new MainParts(2,2).setPartName("Memory").setPartStat(10.0f).setPartConversion("GB");
public static final Parts Fan = new MainParts(3,3).setPartName("Fan").setPartStat(2.0f).setPartConversion("CPUs/Fan");
public static void Init_MainParts(){
Parts.Init_Parts();
setPartArrayStat(CPUStats,CPU);
setPartArrayConversion("GHz",CPU);
Variables.HackLog.info("Parts initialized.");
}
public MainParts(int id, int textureid) {
super(id, textureid);
}
/**
* This creates an array of a new part with the same id, still crashes if you create a single part or an array part with same id later on.
* @param id The id to give to new part
* @param texture The texture index to assign to part
* @param size The size of the array
* @param name To give to the array
* @return
*/
public static Parts[] CreatePartArray(int id, int texture,int size,String name)
{
if(PartsList[id] != null){
Variables.HackLog.severe("Part id of " + id + " is already used.");
String partname = PartsList[id].getPartName();
if(partname != "Un-named"){
throw new IllegalArgumentException("id " + String.valueOf(id) + " was trying to take up part id of " + id + " which is already taken up by "+PartsList[id].getPartName() + ".");
}else{
throw new IllegalArgumentException("id " + String.valueOf(id) + " was trying to take up part id of " + id + " which is already used.");
}
}else{
Parts temp = new MainParts(id,texture);
Parts[] array = new Parts[1];
Parts[] rarray = Arrays.copyOf(array, array.length + size);
rarray[1] = temp;
for(int i=2;i<rarray.length;i++){
try{
rarray[i]=(MainParts)temp.clone();
}catch(Exception e){
e.printStackTrace();
}
}
PartsList[id] = rarray[1]; //Here is what works but not what I wanted.
ArrayList.put(id, true);
setPartArrayName(name,rarray);
return rarray;
}
}
}
这就是我希望它看起来像但不起作用的东西:
public class MainParts extends Parts{
public static Parts[] CPU = CreatePartArray(1,1,4,"CPU");
public static final float[] CPUStats = {1.28f,2.0f,3.36f,5.0f};
public static final Parts Memory = new MainParts(2,2).setPartName("Memory").setPartStat(10.0f).setPartConversion("GB");
public static final Parts Fan = new MainParts(3,3).setPartName("Fan").setPartStat(2.0f).setPartConversion("CPUs/Fan");
public static void Init_MainParts(){
Parts.Init_Parts();
setPartArrayStat(CPUStats,CPU);
setPartArrayConversion("GHz",CPU);
Variables.HackLog.info("Parts initialized.");
}
public MainParts(int id, int textureid) {
super(id, textureid);
}
/**
* This creates an array of a new part with the same id, still crashes if you create a single part or an array part with same id later on.
* @param id The id to give to new part
* @param texture The texture index to assign to part
* @param size The size of the array
* @param name To give to the array
* @return
*/
public static Parts[] CreatePartArray(int id, int texture,int size,String name)
{
if(PartsList[id] != null){
Variables.HackLog.severe("Part id of " + id + " is already used.");
String partname = PartsList[id].getPartName();
if(partname != "Un-named"){
throw new IllegalArgumentException("id " + String.valueOf(id) + " was trying to take up part id of " + id + " which is already taken up by "+PartsList[id].getPartName() + ".");
}else{
throw new IllegalArgumentException("id " + String.valueOf(id) + " was trying to take up part id of " + id + " which is already used.");
}
}else{
Parts temp = new MainParts(id,texture);
Parts[] array = new Parts[1];
Parts[] rarray = Arrays.copyOf(array, array.length + size);
rarray[1] = temp;
for(int i=2;i<rarray.length;i++){
try{
rarray[i]=(MainParts)temp.clone();
}catch(Exception e){
e.printStackTrace();
}
}
PartsList[id] = (Parts)rarray; //This is what I want it to look like but sadly, errors :(
ArrayList.put(id, true);
setPartArrayName(name,rarray);
return rarray;
}
}
}
对于我希望它看起来的那个给出了这个错误:
Type mismatch: cannot convert from Parts[] to Parts
答案 0 :(得分:1)
rarray
属于Parts[]
类型,零件对象数组。您正在尝试将其转换为单个Parts
对象。你怎么能这样做呢?您可以从数组中获取一些元素并将其分配给某个Parts
对象,例如PartsList[id] = rarray[0]
。但这只是一个例子,我真的不知道你想在这里分配什么。