Java creating a new object

I have an abstract class Specie and then an Animals class that extends the species and then my classes for animals (eg sheep). In animals, I have a method that checks if two objects are in the same position on the map, and if they are of the same type (eg sheep and sheep).

If so, it creates another Sheep. And I have a problem with this I tried something like this

Specie new_specie = this;

      

And I have a copy constant in my sheep class

     public Sheep(Sheep new_sheep){

     this(new_sheep.get_x(),new_sheep.get_y(), new_sheep.get_img());
}

      

And then store it in an array that contains all the objects

species[speciesAmount] = new_specie;

      

But it just stores the same object in two array elements

species[0]
species[2]

      

the same object. Any ideas?

But there is another problem: I will have more species of animals (equiv. Wolf), and I cannot do

new Specie(this)

      

because it is an abstract class.

How do I make it call the appropriate constructor?

change. I solved it, I used the clone () method.

+3


source to share


3 answers


Specie new_specie = this;

will just create a new link called new_specie

and let it refer to the same object asthis

. Hence species[speciesAmount] = new_specie;

will assign the same reference to the array entry (which is itself a reference).

Your code doesn't call the copy constructor at all. Unlike C ++ in Java, the call to the copy constructor should look like this:

Specie new_specie = new Sheep(this);

      

Btw, you can look at the interface Clonable

that is for copying objects.



Update . As for your second question, the solution depends on what you want to achieve. If you just want a copy of a specific kind that is in the field when there are two of them, you can simply compare the classes of both kinds (using the methodgetClass()

) or check for equality with equals()

(depends on how you define that it is often more than equal classes).

Then you just call Species new_species = species_on_field.clone()

and get a clone of what was in that field before.

Another option could be to use a factory pattern or prototype based on the class of the current view in that field. I'll leave them for you to watch on the net though.

+3


source


In Java, you cannot call an assignable copy constructor. You have to call it!



species[0] = new Sheep(this);

      

+2


source


You can compare views by comparing their class, for example:

if (specieA.getClass() == specieB.getClass) {

      

Then your next step is to create a new view. At runtime, you can access this information using reflections. It's not exactly pretty, but sometimes it can't be avoided (for example, you don't want to create a switch case for each instance):

    Class<?> clazz = specieA.getClass();
    Specie newSpecie = clazz.getConstructor(clazz).newInstance(specieA);
}

      

This looks for a constructor of the specieA class (named clazz here) that takes an instance of clazz as its input. That way, you should be able to clone everything (assuming it has a copy constructor, of course). If they are not there, an exception will be thrown (you need try-catch).

+1


source







All Articles