【问题标题】:Getting method from HashMap get() return从 HashMap 获取方法 get() 返回
【发布时间】:2017-11-06 15:11:02
【问题描述】:

我目前在处理 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){ ... }
}

我在想的是,当我从地图中“获取”一个值时,我应该能够从那里访问它的方法。 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
    }
}

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

【问题讨论】:

  • 您的问题是什么? “处理 3”是什么意思?
  • @AriaPahlavan Processing 是 Java 的一个子集,我猜你可以这么说。我的问题在帖子底部有明确的措辞。
  • @shmosel 我已更新为尽可能提供最小且完整的示例。至于可验证,因为我的问题是关于为什么某些东西不起作用,显然它是不可验证的。我在问为什么它不起作用,以及我能做些什么。
  • 这些类都在同一个包中吗?

标签: java hashmap processing


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

【讨论】:

  • 虽然这不是一个坏主意,但在构造函数中找出优势,但这并不能解释为什么我不能从地图的 get 方法中获取 Chomosome 的方法或如何解决这个问题。
  • 其实当你调用genes.get("color").getDomGene();它应该返回Chomosome类的第一个参数,对吧?现在它正在退货。我只是在名称中打印了方法来显示循环。
  • 我明天试试这个。
  • 奇怪的是,在今天早上简单地调用genes.get("color").getDomGene() 之后,它就可以工作了!一定是把它错误地称为某事。将其标记为答案,因为有点清理问题,让我再试一次。
【解决方案2】:

如果你把类 Flower 放在不同的包里?您看不到非“公开”的方法。尝试将所有类放在同一个包中或公开方法

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    相关资源
    最近更新 更多