我认为显而易见的解决方案是在任何地方调用.clone.copy
,但有些应用程序在没有.copy
的情况下调用.clone
可能会有危险或难以调试。
似乎demonstrate clone
clone
(或被覆盖的copy
)似乎未被class Process {
def replicate: Process = {
println("Hello from Process.replicate")
new Process()
}
}
final case class Processes(
process1: Process,
process2: Process
) {
override def clone: Processes = Processes(
process1.replicate, process2.replicate)
}
val origProcesses = Processes(new Process, new Process)
val clonedProcesses = origProcesses.copy(process1 = new Process)
使用:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "header.h"
Body nouveauCorps(Body body, Vector3 force, double deltaT){
double m = body.mass;
double t = deltaT;
//Newton's second law:
double ax = force.x/m;
double ay = force.y/m;
double az = force.z/m;
body.speedx += ax*t;
body.speedy += ay*t;
body.speedz += az*t;
body.x +=t*body.speedx;
body.y +=t*body.speedy;
body.z +=t*body.speedz;
return body;
}
int main()
{
//Initial conditions:
double posX = 1.4568899;
double posY = 5.6584225;
double posZ = -8.8944444;
double speedX = 0.232323;
double speedY = -1.6565656;
double speedZ = -8.6565656;
double mass = 558.74;
//Force applied:
Vector3 force = {5.8745554, -97887.568, 543.5875};
Body body = {posX, posY, posZ, speedX, speedY, speedZ, mass};
double duration = 10.0;
double pointsPS = 100.0; //Points Per Second
double pointsTot = duration * pointsPS;
char name[20];
sprintf(name, "BN_%fs-%fpts.txt", duration, pointsPS);
remove(name);
FILE* fichier = NULL;
fichier = fopen(name, "w");
for(int i=1; i<=pointsTot; i++){
body = nouveauCorps(body, force, duration/pointsTot);
double t = i/pointsPS;
//Make a table: TIME | POS_X, Y, Z by simulation | POS_X, Y, Z by modele (reference)
fprintf(fichier, "%e \t %e \t %e \t %e \t %e \t %e \t %e\n", t, body.x, body.y, body.z, force.x*(t*t)/2.0/mass + speedX*t + posX, force.y*(t*t)/2.0/mass + speedY*t + posY, force.z*(t*t)/2.0/mass + speedZ*t + posZ);
}
return 0;
}
答案 0 :(得分:2)
您可以提供自己的复制方法:
final case class Processes(
process1: Process,
process2: Process
) {
override def clone: Processes = Processes(
process1.replicate, process2.replicate)
def copy(process1: Process = this.process1.replicate, process2: Process = this.process2.replicate): Processes = Processes(
process1, process2)
}
在这种情况下,您不会获得Scala提供的“免费”copy
方法,但您仍然可以避免实施自己的equals
,hashCode
和随播广告对象{{ 1}},apply
等。