更改 object 数组的每个元素的属性
Changing the attributes of each element of an object array
我有一些概念上的疑问,很抱歉,如果它们很简单,但我被卡住了。
在模拟星系形成过程中,我有一个名为 SolarSystem
的 class。
class SolarSystem
{
private:
double sPositionX, sVelocityX, sPositionY;
double sVelocityY, sMass, sExcentricity;
public:
//Methods like getPosition(), etc
}
在我的主要函数中,我有 object 数组 SolarSystem systems[1000]
。我需要实现一个算法来使太阳系在我的星系中移动(遵循牛顿定律),因此,我必须更改数组中每个成员的属性值,如位置、速度等。(N-Body问题)。
- 我想我不能制作一个 方法
algorithm()
来改变值,因为系统之间存在依赖关系,并且方法不能考虑数组的其他成员。
- 但是,如果我选择更改 函数 中的属性,我
需要那些是 public,然后,有没有理由做一个
class?在我的主函数
int main()
中将属性作为变量 positionX[i], mass[i], etc
不是更容易吗?
类 允许您以有意义的方式构建数据,提高可扩展性、可维护性并使您的代码更易于理解。如果您的主要问题是从 class 访问 systems
,您可以将其作为参数传递。在现代 C++ 中,您应该使用 std::vector
或 std::array
而不是 C 风格的数组。例如:
class SolarSystem
{
private:
double sPositionX, sVelocityX, sPositionY;
double sVelocityY, sMass, sExcentricity;
public:
// getters and setters and stuff
void algorithm(const std::vector<SolarSystem> &otherSystems) {
for(const auto &otherSystem : otherSystems) {
// iterate through all systems...
}
}
}
如果您也将太阳能系统存储在 std::vector
中,例如
std::vector<SolarSystem> solarSystems;
您可以遍历它并像这样更新所有系统:
for(auto &system : solarSystems) {
system.algorithm(solarSystems);
}
例如,您可以简单地创建另一个名为 "Galaxy" 的 class,其中包含您的系统。
#include <vector>
#include <SolarSystem.h>
Galaxy {
std::vector<SolarSystem> system;
public :
//construtor and destructor
void algorithm();
//some other methods like this :
void addSolarSystem(SolarSystem& system);
};
最后,在 algorithm 中,您可以使用 SolarSystem class 中的 getter 和 setter(或方法: 加速(浮点值), ...).
最好再创建一个 class,因为这是现实:星系包含太阳系,这个容器 (星系) 链接您的依赖项。此外,您应该始终应用 "Single-responsibility principle" wiki :您的太阳系不能影响其他系统,只能影响进入该系统的行星(单一责任)。如果没有这个原则,当你实现其他东西(彗星......)时会遇到一些问题。
通常情况下,最好封装你的数据和函数。
我有一些概念上的疑问,很抱歉,如果它们很简单,但我被卡住了。
在模拟星系形成过程中,我有一个名为 SolarSystem
的 class。
class SolarSystem
{
private:
double sPositionX, sVelocityX, sPositionY;
double sVelocityY, sMass, sExcentricity;
public:
//Methods like getPosition(), etc
}
在我的主要函数中,我有 object 数组 SolarSystem systems[1000]
。我需要实现一个算法来使太阳系在我的星系中移动(遵循牛顿定律),因此,我必须更改数组中每个成员的属性值,如位置、速度等。(N-Body问题)。
- 我想我不能制作一个 方法
algorithm()
来改变值,因为系统之间存在依赖关系,并且方法不能考虑数组的其他成员。 - 但是,如果我选择更改 函数 中的属性,我
需要那些是 public,然后,有没有理由做一个
class?在我的主函数
int main()
中将属性作为变量positionX[i], mass[i], etc
不是更容易吗?
类 允许您以有意义的方式构建数据,提高可扩展性、可维护性并使您的代码更易于理解。如果您的主要问题是从 class 访问 systems
,您可以将其作为参数传递。在现代 C++ 中,您应该使用 std::vector
或 std::array
而不是 C 风格的数组。例如:
class SolarSystem
{
private:
double sPositionX, sVelocityX, sPositionY;
double sVelocityY, sMass, sExcentricity;
public:
// getters and setters and stuff
void algorithm(const std::vector<SolarSystem> &otherSystems) {
for(const auto &otherSystem : otherSystems) {
// iterate through all systems...
}
}
}
如果您也将太阳能系统存储在 std::vector
中,例如
std::vector<SolarSystem> solarSystems;
您可以遍历它并像这样更新所有系统:
for(auto &system : solarSystems) {
system.algorithm(solarSystems);
}
例如,您可以简单地创建另一个名为 "Galaxy" 的 class,其中包含您的系统。
#include <vector>
#include <SolarSystem.h>
Galaxy {
std::vector<SolarSystem> system;
public :
//construtor and destructor
void algorithm();
//some other methods like this :
void addSolarSystem(SolarSystem& system);
};
最后,在 algorithm 中,您可以使用 SolarSystem class 中的 getter 和 setter(或方法: 加速(浮点值), ...).
最好再创建一个 class,因为这是现实:星系包含太阳系,这个容器 (星系) 链接您的依赖项。此外,您应该始终应用 "Single-responsibility principle" wiki :您的太阳系不能影响其他系统,只能影响进入该系统的行星(单一责任)。如果没有这个原则,当你实现其他东西(彗星......)时会遇到一些问题。
通常情况下,最好封装你的数据和函数。