简体   繁体   中英

Getting method from HashMap get() return

I am currently working in Processing 3, and am having troubles understanding the return of a HashMap. I have a map, Map<String, Chromosome> genes = new HashMap<String, Chromosome>() which uses my classes,

class Chromosome{
  Genotype geneOne;
  Genotype geneTwo;

  Chromosome(){ ... }

  Chromosome(Genotype gOne, Genotype gTwo){ ... }

  void setGeneOne(Genotype gene){ ... }

  void setGeneTwo(Genotype gene){ ... }

  Genotype getDomGene(){ ... }

  Genotype getRecGene(){ ... }
}

class Genotype{
  Object value;
  float weight;

  public Genotype(int value, float weight){ ... }

  public Genotype(int[] value, float weight){ ... }

  public Genotype(String value, float weight){ ... }

  public Genotype(float value, float weight){ ... }

  public Object getValue(){ ... }

  public float getWeight(){ ... }

  public void setValue(int value){ ... }

  public void setValue(int[] value){ ... }

  public void setValue(String value){ ... }

  public void setValue(float value){ ... }
}

What I'm thinking is that when I "get" a value from the map, I should be able to access its methods from there. IE

class Flower{
    Map<String, Chromosome> genes;
    Flower(){
        genes = new HashMap<String, Chromosome>();
        genes.put("color", new Chromosome(new Genotype(64, 1.0), new Genotype(25,0.5)));
        Genotype test = genes.get("color").getDomGene(); //should return the first param passed to the new chromosome
    }
}

I'm hoping to avoid having to declare the returned object every time I use it. From all of 20 minutes of googling, I can't seem to find anything about this working, so why does this not work, and what can be done to work around it?

You should just return genOne in getDomGene method.

Chromosome class.

package gen;

class Chromosome {

    Genotype geneOne;
    Genotype geneTwo;

    Chromosome() {
        System.out.println("Chromosome.Chromosome");
    }

    Chromosome(Genotype gOne, Genotype gTwo) {
        System.out.println("Chromosome.Chromosome");
    }

    void setGeneOne(Genotype gene) {
        System.out.println("Chromosome.setGeneOne");
    }

    void setGeneTwo(Genotype gene) {
        System.out.println("Chromosome.setGeneTwo");
    }

    Genotype getDomGene() {
        System.out.println("return genOne");
        return geneOne;
    }

    Genotype getRecGene() {
        System.out.println("return genTwo");
        return geneTwo;
    }
}

Genotype class

package gen;

class Genotype {

    Object value;
    float weight;

    public Genotype(int value, float weight) {
        System.out.println("Genotype.Genotype");
    }

    public Genotype(int[] value, float weight) {
        System.out.println("Genotype.Genotype");
    }

    public Genotype(String value, float weight) {
        System.out.println("Genotype.Genotype");
    }

    public Genotype(float value, float weight) {
        System.out.println("Genotype.Genotype");
    }

    public Object getValue() {
        System.out.println("Genotype.getValue");
        return null;
    }

    public void setValue(String value) {
        System.out.println("Genotype.setValue");
    }

    public void setValue(float value) {
        System.out.println("Genotype.setValue");
    }

    public void setValue(int value) {
        System.out.println("Genotype.setValue");
    }

    public void setValue(int[] value) {
        System.out.println("Genotype.setValue");
    }

    public float getWeight() {
        System.out.println("Genotype.getWeight");
        return 0;
    }
}

Flower class.

package gen;

import java.util.HashMap;
import java.util.Map;

class Flower {

    Map<String, Chromosome> genes;

    Flower() {
        genes = new HashMap<>();
        genes.put("color", new Chromosome(new Genotype(64, 1.0f), new
                Genotype(25, 0.5f)));
        Genotype test = genes.get("color")
                .getDomGene(); //should return the first param passed to the new chromosome
    }

    public static void main(String[] args) {
        new Flower();
    }
}

It prints

Genotype.Genotype
Genotype.Genotype
Chromosome.Chromosome
return genOne

return genOne means that you have an access to the geneOne field of Chromosome class, that is its first parameter.

if you put class Flower in a different package? you can't see methods that are not "public". try putting all classes in the same package or make the method public

public Genotype getDomGene(){ ... }

public Genotype getRecGene(){ ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM