简体   繁体   English

从HashMap获取方法get()返回

[英]Getting method from HashMap get() return

I am currently working in Processing 3, and am having troubles understanding the return of a HashMap. 我目前在处理3中工作,无法理解HashMap的返回。 I have a map, Map<String, Chromosome> genes = new HashMap<String, Chromosome>() which uses my classes, 我有一个地图, Map<String, Chromosome> genes = new HashMap<String, Chromosome>()使用我的类的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){ ... }
}

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 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? 在整个20分钟的搜索过程中,我似乎都找不到任何有关此工作的信息,那么为什么这行不通,如何解决呢?

You should just return genOne in getDomGene method. 您应该只在getDomGene方法中返回genOne。

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. return genOne意味着您可以访问Chromosome类的geneOne字段,这是它的第一个参数。

if you put class Flower in a different package? 如果您将Class Flower放在其他包装中? 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(){ ... } 公共基因型getDomGene(){...}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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