我有一个Bicycle继承自的子类Agent.代理人具有依赖于自行车来定义它的属性.也就是说,代理的物理模型需要用速度和加速度约束来初始化,速度和加速度约束是基于每个自行车定义的,并且对于另一种类型的代理将是不同的.
我遇到的问题是我无法在base()构造函数中传递我需要计算的参数(速度/加速度需要计算以从理论分布中绘制它们),因为子类当然还没有被实例化.
每个自行车实例进行一次计算,但是多次使用,因此简单的静态方法无法完成工作.我可以protected在计算后调用父方法中的一个方法,但AFAIK无法在孩子中强制执行此方法,或者更具体地说,在我将来可能不写的任何孩子中.
例如,我可以:
public abstract class Agent
{
protected IPhysics PluginPhysics { get; set; }
protected Agent(...)
{
}
}
public class Bicycle : Agent
{
private double maxA;
public Bicycle(Object anotherParameter) : base(...)
{
maxA = ComputationOfMaxA();
this.PluginPhysics = new Physics(anotherParameter, maxA);
}
private static double ComputationOfMaxA()
{
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
或者我可以:
public abstract class Agent
{
protected IPhysics PluginPhysics { get; private set; }
protected Agent(...)
{
} …Run Code Online (Sandbox Code Playgroud) 以下代码:
#include<stdlib.h>
#include<stdio.h>
int main (void) {
FILE** f;
if ( (*f = (FILE *)malloc( sizeof(FILE *)) ) == NULL) {
printf("Out of RAM or some other disaster!\n");
return 1;
}
printf("OK!\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在Mac OS X 10.8上编译和运行无怨言.但是在Windows 7(使用MinGW编译)上,它会在malloc()上崩溃.为什么这会和/或任何想法阻止它发生?
谢谢!
注意:这显然是一个较大的程序的一部分,但我已经将整个程序减少到上面,并在Mac和PC上尝试了这个代码,并复制了行为.