【问题标题】:Java OOP conception problemJava OOP 概念问题
【发布时间】:2011-04-02 12:10:22
【问题描述】:

我对 java OOP 概念很陌生。我在用java实现一些关于遗传学的东西时遇到了一些问题。这是场景。

一个人体细胞包含 23 对染色体。每对中的每条染色体都有数千个基因。基因的每个拷贝称为等位基因。这些等位基因决定了人类的不同特征(眼睛颜色、身高、头发颜色、遗传疾病等)。等位基因可以是显性的也可以是隐性的。

显性等位基因总是在其承载者的基因组中表达。然而,如果当同一基因的显性等位基因存在时,一个等位基因的信息不表达,则它是隐性等位基因。基因的隐性等位基因的特点是它可以存在于基因组中并在几代人中传播,而不会在其承载者的表型中表达。如果没有显性等位基因,则该基因的两个拷贝具有相同的隐性等位基因(homozygous 隐性),则表示隐性特征。

所以现在,在孩子细胞的一对染色体上,一个染色体来自母亲,另一个来自父亲。因此,假设在父亲染色体的位置 1 包含负责眼睛颜色的基因的等位基因(显性或隐性),在母体染色体的相同位置上必须包含同一基因的另一个等位基因(显性或隐性)。 那么我怎样才能正确地表示这些类等位基因,基因呢?这是我到目前为止所拥有的

Allele.java
    /**
 * 
 */
package project_try;

/**
 * @author mkab
 *
 */
public class Allele {

    private String trait;
    private int type;

    public Allele(String trait, int type) {
        this.trait = trait;
        this.type = type;
    }

    public boolean equals(Allele a){
        if(this.getTrait().equals(a.getTrait()) && this.getType()==a.getType())
            return true;

        return false;
    }

    /**
     * @return the trait
     */
    public String getTrait() {
        return trait;
    }

    /**
     * @param trait the trait to set
     */
    public void setTrait(String trait) {
        this.trait = trait;
    }

    /**
     * @param type the type to set
     */
    public void setType(int type) {
        this.type = type;
    }

    /**
     * @return the type either recessive(0) or dominant(1)
     */
    public int getType() {
        return type;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Allele [trait=" + trait + ", type=" +
        (this.getType()==0?"recessive":"dominant")+ "]";
    }
}

Gene.java

    /**
 * 
 */

package project_try;

import java.util.Random;

/**
 * @author mkab
 *
 */
public class Gene implements Cloneable {

    private Allele allele;

    public Gene(){
        super();
    }

    public Gene(Allele allele){
        super();
        this.allele = allele;
    }

    /**
     * Randomly selects a trait from trait1 or trait2 and returns a new Gene with that trait
     * @param trait1
     * @param trait2
     * 
     * @return a new Gene
     */
    public Gene randomAllele(Allele trait1, Allele trait2){
        Allele allele = null;
        Random rand = new Random();
        int i = rand.nextInt(2);// generate between 0 and 2: only 2 possibilities: 0 or 1

        switch(i){
        case 0:
            allele = trait1;
            break;

        case 1:
            allele = trait2;
            break;
        }

        return new Gene(allele);
    }

    /**
     * Clones a gene
     */
    public Gene clone() throws CloneNotSupportedException{
        Gene g;
        g = (Gene) super.clone();
        return g;
    }

    public boolean equals(Gene g){
        if(this.getAllele().equals(g.getAllele()) && 
                this.getAlleleType() == g.getAlleleType() )
            return true;
        return false;
    }

    /**
     * @param allele the allele to set
     */
    public void setAllele(Allele allele) {
        this.allele = allele;
    }

    /**
     * 
     * @return - the allele 
     */
    public Allele getAllele() {
        return allele;
    }

    /**
     * 
     * @return the trait of the gene or allele
     */
    public String getAlleleTrait(){
        return allele.getTrait();
    }

    /**
     * 
     * @return allele type. Either recessive or dominant
     */
    public int getAlleleType(){
        return allele.getType();
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Gene [" + allele +"]";
    }
}

我怎样才能表示类染色体?我正在考虑使用基因类型的数组列表。但是使用它的问题是我无法定义例如数组的第一个位置应该用于眼睛颜色,第二个位置用于高度等。 请问有什么建议吗?谢谢

【问题讨论】:

    标签: java oop


    【解决方案1】:

    我看不出有什么问题

    List<Chromosomes> chromosomes = new ArrayList<Chromosomes>();
    

    定义

    class Chromosome {
        // values
    }
    
    class EyeColourChromosome extends Chromosome {
        // define specific values
    }
    
    
    class HeightChromosome extends Chromosome {
        // define specific values
    }
    

    或者……

    您可以使用工厂模式来创建特定的染色体,例如

    class Chromosome {
        // values
        public static Chromosome chromosomeFactory(String type) {
            switch(type) {
            case EYE_COLOUR:
                return new EyeColourChromosome(...);
                break;
            case HEIGHT:
                return new HeightChromosome(...);
                break;
            }
        }
    }
    

    【讨论】:

    • Chromosome 应该是 IMO 接口
    • 这就是我的想法。 Chromosome 应该是接口还是抽象类。但问题是染色体应该包含基因。使用上述方法,我将拥有包含染色体的染色体。
    【解决方案2】:

    也许使用一个LinkedHashMap,其中的关键是他的眼睛颜色、高度等,而值是……值。使用 LinkedHashMap 意味着您仍然保持排序,因为这似乎很重要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多