让我们首先考虑Gene
该类:根据您的规范(我有一个 Gene 是 char [] 和四个数字),您需要一个char
数组作为该类的属性。而且,这个类应该是可克隆的,那么你必须让这个类实现Cloneable
接口:为此,你必须声明这个Gene
类实现了Cloneable
接口(简单地写implements Cloneable
在类定义中)并且你必须实现clone
这个类中的方法(在此方法中,您必须制作对象字段的深层副本并返回克隆的对象,请参阅下面的代码了解详细信息)。
import java.util.Arrays;
/*
* Definition of the class that also includes the declaration
* of the implementation of the Cloneable interface.
*/
public class Gene implements Cloneable {
/*
* The length of a gene.
* It is defined as constant (final) in order to use the same value
* in the whole class, where and when necessary.
*/
private static final int GENE_LENGTH = 4;
/*
* In biology a gene it corresponds to a sequence of nucleic acids,
* so I thought of naming m_sequence this field.
*/
private char m_sequence[];
/*
* This constructor allows you to instantiate a new object from a char array.
*/
public Gene(char sequence[]) {
// The field m_sequence is initialized with a copy
// of the array specified in the constructor.
m_sequence = Arrays.copyOf(sequence, GENE_LENGTH);
}
/*
* Simple getter method.
* Since m_sequence is private, you need a method like this
* in order to access elements of the array.
*/
public char getUnit(int index) {
return m_sequence[index];
}
/*
* Simple setter method.
* Since m_sequence is private, you need a method like this
* in order to set the elements of the array.
*/
public void setUnit(int index, char unit) {
m_sequence[index] = unit;
}
/*
* The Cloneable declaration requires that this class has clone method.
* This method should return an Gene object within an Object.
*/
protected Object clone() throws CloneNotSupportedException {
// First, we invoke the clone method of the superclass
Gene clone = (Gene)(super.clone());
// Then, make the deep copy of the object.
// In this case the only field present in the Gene object is an array,
// then you must make a deep copy of this array: in order to make a deep
// copy of the array, you should use the Arrays.copyOf method.
clone.m_sequence = Arrays.copyOf(m_sequence, GENE_LENGTH);
return clone;
}
/*
* Get a representation of this object as a String.
* Just a method for simple testing.
*/
@Override
public String toString() {
return Arrays.toString(m_sequence);
}
}
请注意,为了复制数组,我使用了类的方法copyOf
(有关数组复制的更多详细信息,Arrays
请阅读此处)。
一个简单的测试来检查Gene
对象中深拷贝的功能:
public static void main(String args[]) throws CloneNotSupportedException {
Gene g1 = new Gene(new char[]{'a', 'b', 'c', 'd'});
Gene g2 = (Gene)(g1.clone());
// now Let's modify g1
g1.setUnit(0, 'e');
g1.setUnit(1, 'f');
g1.setUnit(2, 'g');
g1.setUnit(3, 'h');
System.out.println("g1: " + g1);
System.out.println("g2: " + g2); // g2 has not changed
}
所以,在你应该改变你的createChild
方法如下。
public class Chromosome {
private static final int CHROMOSOME_LENGTH = 10;
/* Array of Gene object. */
private Gene genes[];
/* Default constructor. */
public Chromosome() {
// Simply allocates an array of 10 elements.
genes = new Gene[CHROMOSOME_LENGTH];
}
/*
* Simple getter method.
* Since m_Genes is private, you need a method like this
* in order to access elements of the array.
*/
public Gene getGene(int index) {
return genes[index];
}
/*
* Simple setter method.
* Since m_Genes is private, you need a method like this
* in order to set the elements of the array.
*/
public void setGene(int index, Gene gene) {
genes[index] = gene;
}
/* The method which make the cross-over. */
public Chromosome createChild(Chromosome parentA, Chromosome parentB) throws CloneNotSupportedException {
Chromosome child = new Chromosome();
// make the cross-over
for (int i=0;i<10; i++)
{
if (i<5)
{
// you need to call the clone() method
child.genes[i] = (Gene)(parentA.genes[i].clone());
}
else
{
// you need to call the clone() method
child.genes[i] = (Gene)(parentB.genes[i].clone());
}
}
return child;
}
}