从HashMap获取方法get()返回

时间:2017-11-06 04:50:18

标签: java hashmap processing

我目前正在处理Processing 3,并且很难理解HashMap的返回。我有一张地图,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){ ... }
}

我在想的是,当我从地图“获取”一个值时,我应该可以从那里访问它的方法。 I.E.

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
    }
}

我希望每次使用它时都要避免声明返回的对象。从谷歌搜索的所有20分钟,我似乎无法找到任何关于这项工作,所以为什么这不起作用,可以做些什么来解决它?

2 个答案:

答案 0 :(得分:1)

您应该在getDomGene方法中返回genOne。

染色体类。

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;
    }
}

基因型课程

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;
    }
}

花卉课程。

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表示您可以访问geneOne类的Chromosome字段,这是它的第一个参数。

答案 1 :(得分:0)

如果你把花类放在不同的包装里?你看不到那些不“公开”的方法。尝试将所有类放在同一个包中或使方法公开

public Genotype getDomGene(){...}

公共基因型getRecGene(){...}