我对java面向对象概念还很陌生。我在java中实现一些关于遗传学的东西时遇到了一些问题。这是一个场景。
人类细胞包含23对染色体。每对染色体中的每一条都有数千个基因。基因的每个拷贝都被称为等位基因。这些等位基因决定了人类的不同特征(眼睛颜色、身高、头发颜色、遗传病等)。等位基因可以是显性的,也可以是隐性的。
显性等位基因总是在其携带者的基因组中表达。然而,当同一基因的显性等位基因存在时,如果来自一个等位基因的信息没有表达出来,那么它就是隐性等位基因。一个基因的隐性等位基因的独特之处在于,它可以存在于基因组中,并在没有在其携带者的表型中表现出来的情况下传递给几代人。如果没有显性等位基因,则该基因的两个副本具有相同的隐性等位基因(纯合隐性),则隐性性状表现出来。
现在,在孩子细胞的一对染色体上,一条染色体来自母亲,另一条来自父亲。因此,假设在父亲染色体的第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 +"]";
}
}我如何也表示类染色体呢?我在考虑使用基因类型的数组列表。但是使用它的问题是,我不能定义例如数组的第一个位置应该用于眼睛颜色,第二个位置用于高度等。有什么建议吗?谢谢
发布于 2011-04-02 20:35:03
我看不出有什么问题
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;
}
}
}发布于 2011-04-02 20:38:22
也许用一个LinkedHashMap,这里的关键是他什么眼睛的颜色,身高等等,值是...值。使用LinkedHashMap意味着您仍然保持排序,因为这似乎很重要。
https://stackoverflow.com/questions/5523056
复制相似问题