Konstruktor - jedes Objekt einzeln erzeugen - alternative?

  • Themenstarter Themenstarter David Nienhaus
  • Beginndatum Beginndatum
D

David Nienhaus

Gast
Hallo,

ich frage mich gerade, ob es eine elegantere Lösung gibt, mehrere Objekte in einem Konstruktor quasi auf einmal zu erzeugen. In meinem konkreten Beispiel: Ich möchte 21 verschiedene Inventare erzeugen, die mit i1-i21 durchnummeriert sein sollen.
Momentan sieht das ganze bei mir so aus:

Code:
Inventar i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13, i14, i15, i16, i17, i18, i19, i20, i21;
       
        i1 = new Inventar();
        i2 = new Inventar();
        i3 = new Inventar();
        i4 = new Inventar();
        i5 = new Inventar();
        i6 = new Inventar();
        i7 = new Inventar();
        i8 = new Inventar();
        i9 = new Inventar();
        i10 = new Inventar();
        i11 = new Inventar();
        i12 = new Inventar();
        i13 = new Inventar();
        i14 = new Inventar();
        i15 = new Inventar();
        i16 = new Inventar();
        i17 = new Inventar();
        i18 = new Inventar();
        i19 = new Inventar();
        i20 = new Inventar();
        i21 = new Inventar();

Das geht doch besitmmt irgendwie mit einer for-Schleife, oder? Ich wüsste allerdings nicht wie.
 
Array + for-Schleife sind hier das Stichwort.
Erstell dir ein Inventar-Array der Länge 21 und initialisier in der for-Schleife jedes einzelne Element mit einem neuen Inventar-Objekt.
 
@David Nienhaus
Dabei denke ich an eine Liste<ObjectDeinerWahl>. Diese kannst du dann a) in der Klasse füllen oder b) eine Factory bauen, wenn die Inventare wo anders ebenso Verwendung finden und dahin geladen werden sollen. Liste aus einem einfachen Grund: viel bequemer!
 
Meinst du so? Klappt irgendwie noch nicht 😀

Code:
Inventar[] meinArray = new Inventar[21];
       
        for (int i=0; i<meinArray.length(); i++)
        {
               meinArray[i] = new Inventar();
        }
 
Er kennt die Methode .length() nicht .. Ich habe aber java.util.*; importiert.
Woran könnte es liegen?
 
@David Nienhaus
When my wife let me go to fishing..
Eventuell würde ich es so gestalten. Eine Option, das Inventar durch eine extra Klasse zu laden, steht nach wie vor offen. Aber als Grundidee fiel mir das hier gerade ein.
Java:
public abstract class Inventory
{

    public abstract String getName();

    public abstract int getID();
}
public class FishingRod extends Inventory implements Comparable<FishingRod>
{
    private final String name = "Fishing Rod";
    private final int id = 1000123;
    @Override
    public String getName()
    {
        return name;
    }
    @Override
    public int getID()
    {
        return id;
    }
 
    @Override
    public int compareTo(FishingRod f)
    {
        if (this.hashCode() > f.hashCode())
        {
            return 1;
        } else if (this.hashCode() < f.hashCode())
        {
            return -1;
        } else
        {
            return 0;
        }
    }
    @Override
    public boolean equals(Object obj)
    {
        if (obj == null)
        {
            return false;
        }
        if (obj == this)
        {
            return true;
        }
        if (!obj.getClass().equals(this.getClass()))
        {
            return false;
        }
        return this.compareTo((FishingRod) obj) == 0;
    }
    @Override
    public int hashCode()
    {
        int hash = 5;
        hash = 83 * hash + Objects.hashCode(this.name);
        hash = 83 * hash + this.id;
        return hash;
    }
}

import java.util.Objects;
public class Chop extends Inventory implements Comparable<Chop>
{
    private final String name = "Chop";
    private final int id = 20000343;
    @Override
    public String getName()
    {
        return name;
    }
    @Override
    public int getID()
    {
        return id;
    }
    @Override
    public int compareTo(Chop o)
    {
        if (this.hashCode() > o.hashCode())
        {
            return 1;
        } else if (this.hashCode() < o.hashCode())
        {
            return -1;
        } else
        {
            return 0;
        }
    }
    @Override
    public boolean equals(Object obj)
    {
        if (obj == null)
        {
            return false;
        }
        if (obj == this)
        {
            return true;
        }
        if (!obj.getClass().equals(this.getClass()))
        {
            return false;
        }
        return this.compareTo((Chop) obj) == 0;
    }
    @Override
    public int hashCode()
    {
        int hash = 5;
        hash = 83 * hash + Objects.hashCode(this.name);
        hash = 83 * hash + this.id;
        return hash;
    }
}


import java.util.Objects;
public class Plummet extends Inventory implements Comparable<Plummet>
{
    private final String name = "Plummet";
    private final int id = 4000234;
    @Override
    public String getName()
    {
        return name;
    }
    @Override
    public int getID()
    {
        return id;
    }
    @Override
    public int compareTo(Plummet p)
    {
        if (this.hashCode() > p.hashCode())
        {
            return 1;
        } else if (this.hashCode() < p.hashCode())
        {
            return -1;
        } else
        {
            return 0;
        }
    }
    @Override
    public boolean equals(Object obj)
    {
        if (obj == null)
        {
            return false;
        }
        if (obj == this)
        {
            return true;
        }
        if (!obj.getClass().equals(this.getClass()))
        {
            return false;
        }
        return this.compareTo((Plummet) obj) == 0;
    }
    @Override
    public int hashCode()
    {
        int hash = 5;
        hash = 83 * hash + Objects.hashCode(this.name);
        hash = 83 * hash + this.id;
        return hash;
    }
}
Java:
package loader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ReadyToFishing
{

    /**
     * Your equipment you need to go to fishing
     */
    private List<Inventory> inventorys = new ArrayList<>();
    /**
     * Your controllist to check your equipment
     */
    private List<Inventory> control = new ArrayList<>();

   
     public ReadyToFishing(Inventory... invent)
    {
        fill(invent);
        control = fill();
    }
    /**
     * Fill your equipment with your fishing stuff
     * @param inventory fishing stuff
     */
    private void fill(Inventory[] inventory)
    {
        inventorys.addAll(Arrays.asList(inventory));
    }

    /**
     * Your checkinglist for fishing
     * @return
     */
    private List<Inventory> fill()
    {
        List<Inventory> x = new ArrayList<>();
        x.add(new FishingRod());
        x.add(new Chop());
        x.add(new Plummet());
        return x;
    }

    /**
     * If you have everything needed to catch a fish, then go;
     *
     * @return true, if {@code inventorys} contains {@code control}
     */
    String canGoFishing()
    {
        return inventorys.containsAll(control) ? "Can go" : "Something missing";
    }

}
Java:
public class Loader
{

    public static void main(String[] args)
    {
        FishingRod fishingRod = new FishingRod();
        Chop chop = new Chop();

        ReadyToFishing notRealyReady = new ReadyToFishing(fishingRod, chop);
        System.out.println(notRealyReady.canGoFishing());

        Plummet plummet = new Plummet();

        ReadyToFishing butNow = new ReadyToFishing(fishingRod, chop, plummet);
        System.out.println(butNow.canGoFishing());
    }

}
/** Output */
Something missing
Can go
 
Zuletzt bearbeitet:

Zurück
Oben