从 HashMap 获取方法 get() return

Getting method from HashMap get() return

我目前正在处理 Processing 3,在理解 HashMap 的 return 时遇到问题。我有一张地图 Map<String, Chromosome> genes = new HashMap<String, Chromosome>(),它使用我的 类、

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){ ... }
}

我在想的是,当我 "get" 来自地图的一个值时,我应该能够从那里访问它的方法。 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
    }
}

我希望避免每次使用 returned 对象时都必须声明它。在 20 分钟的谷歌搜索中,我似乎找不到任何关于此工作的信息,那么为什么这不起作用,可以采取什么措施来解决这个问题?

你应该 return genOne 在 getDomGene 方法中。

染色体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;
    }
}

基因型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;
    }
}

花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();
    }
}

它打印

Genotype.Genotype
Genotype.Genotype
Chromosome.Chromosome
return genOne

return genOne 表示您可以访问 Chromosome class 的 geneOne 字段,这是它的第一个参数。

如果你把 class 花放在不同的包裹里?你看不到不是 "public" 的方法。尝试将所有 classes 放在同一个包中或使方法 public

public 基因型 getDomGene(){ ... }

public 基因型 getRecGene(){ ... }