2017-11-06 5 views
0

현재 Processing 3에서 작업 중이며 HashMap의 반환을 이해하는 데 문제가 있습니다. 나는지도, 내 클래스를 사용 Map<String, Chromosome> genes = new HashMap<String, Chromosome>()을 가지고HashMap get() return 메서드에서 받기

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 분의 인터넷 검색 결과에서이 작업에 대해 아무 것도 찾을 수없는 것 같습니다. 왜이 기능이 작동하지 않으며 그 문제를 해결하려면 무엇을 할 수 있습니까?

+1

는 [mcve]을 제공하십시오를 같은 패키지의 모든 클래스를 넣어 시도하거나 확인 . – shmosel

+1

귀하의 질문은 무엇입니까? "Processing 3"이란 무엇을 의미합니까? –

+0

@AriaPahlavan [Processing] (https://processing.org/)은 Java의 일부라고 할 수 있습니다. 내 질문은 게시물 하단에 분명히 표시되어 있습니다. –

답변

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 필드에 액세스 할 수 있음을 의미 인쇄합니다.

+0

생성자의 우위를 알아내는 것이 나쁜 생각은 아니지만지도의 get 메서드에서 Chomosome의 메서드를 가져올 수없는 이유 또는이 문제를 해결하는 방법에 대한 대답이 아닙니다. –

+0

실제로'genes.get ("color"). getDomGene();을 호출하면 Chomosome 클래스의 첫 번째 매개 변수를 반환해야합니다. 맞습니까? 이제 그것을 돌려 보내고 있습니다. 방금 사이클을 보여주기 위해 이름으로 메소드를 인쇄했습니다. –

+0

나는 이것을 내일 시도 할 것이다. –

0

꽃을 다른 패키지에 넣으면? 당신은 "대중"이 아닌 방법을 볼 수 없습니다. 방법 공공

공공 유전자형 getDomGene() {...}

공공 유전자형 getRecGene() {...}