gpt4 book ai didi

java - 从 HashMap 获取方法 get() 返回

转载 作者:行者123 更新时间:2023-12-02 12:02:40 25 4
gpt4 key购买 nike

我目前正在处理 3 中工作,并且在理解 HashMap 的返回时遇到困难。我有一张 map ,Map<String, Chromosome> genes = new HashMap<String, Chromosome>()它使用我的类(class),

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

我的想法是,当我从 map “获取”一个值时,我应该能够从那里访问它的方法。 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 分钟的谷歌搜索,我似乎找不到任何有关此工作的信息,那么为什么这不起作用,可以采取哪些措施来解决它?

最佳答案

您应该在 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 字段,这是它的第一个参数。

关于java - 从 HashMap 获取方法 get() 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47129924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com