[英]java 8 - store method in HashMap and get return value from method in map
[英]Getting method from HashMap get() return
我目前在处理3中工作,无法理解HashMap的返回。 我有一个地图, 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){ ... }
}
我在想的是,当我从地图中“获取”一个值时,我应该能够从那里访问其方法。 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
字段,这是它的第一个参数。
如果您将Class Flower放在其他包装中? 您看不到不是“公共”的方法。 尝试将所有类放在同一包中或将方法公开
公共基因型getDomGene(){...}
公共基因型getRecGene(){...}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.