-1

首先,让我解释一下我要做什么。在java中,我正在创建一个与我在vb6中制作的高中cca程序之一相关的游戏(已经很好地提交了),但我仅限于我可以在其中做的事情,所以我正在工作用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
4

1 回答 1

1

rarrayParts[]Parts 对象数组的类型。您正在尝试将其转换为单个Parts对象。你怎么看它可以做到?例如,您可以从数组中获取一些元素并将其分配给某个Parts对象PartsList[id] = rarray[0]。但这只是一个例子,我真的不知道你想在这里分配什么。

于 2013-02-03T20:43:35.820 回答