-1

まず、私がやろうとしていることを説明させてください。Javaで、vb6で作成している高校のccaプログラムの1つに関連するゲームを作成しています(すでに十分に手渡されています)が、その中でできることは限られているので、取り組んでいますジャバで。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

rarrayパーツ オブジェクトの配列でParts[]ある型です。これを単一のオブジェクトに変換しようとしています。どうすればそれができるようになりますか?たとえば、配列からいくつかの要素を取得し、それをいくつかのオブジェクトに割り当てることができます。しかし、これは単なる例です。ここで何を割り当てたかったのか、私にはまったくわかりません。PartsPartsPartsList[id] = rarray[0]

于 2013-02-03T20:43:35.820 に答える