public class Human { private final String gene; private final Human father; private final Human mother; public static final Human ADAM = new Human("ATCGAATTCGATCCGG"); public static final Human EVE = new Human("ATCGTTATGGTTGGCC"); // FOR Adam & Eve private Human(String gene) { this.gene = gene; this.father = null; this.mother = null; } public Human(Human father, Human mother) { this.father = father; this.mother = mother; char[] geneArray = new char[father.gene.length]; for (int i = 0; i < gene.length; i++) { gene[i] = random() ? father.gene.charAt(i) : mother.gene.charAt(i); } this.gene = new String(geneArray); } }
class Father {
String gene = "f";
class Mother {
String gene = "m";
}
}
谢谢大大
我去试试看(因为我才刚摸JAVA不久)
自己再写个算法来选择继承父母双方各一半基因
因为关于这个问题即使java支持多继承不也一样是全部继承下来的吗
public class Human {
private final String gene;
private final Human father;
private final Human mother; public static final Human ADAM = new Human("ATCGAATTCGATCCGG");
public static final Human EVE = new Human("ATCGTTATGGTTGGCC"); // FOR Adam & Eve
private Human(String gene) {
this.gene = gene;
this.father = null;
this.mother = null;
} public Human(Human father, Human mother) {
this.father = father;
this.mother = mother;
char[] geneArray = new char[father.gene.length];
for (int i = 0; i < gene.length; i++) {
gene[i] = random() ? father.gene.charAt(i) : mother.gene.charAt(i);
}
this.gene = new String(geneArray);
}
}