我应该在私人项目中使用私人课程吗?

发布于 2024-12-04 10:13:25 字数 1495 浏览 2 评论 0原文

我正面临着为一个研究项目编写一个相当大的程序,该程序可能只会由我使用,或者也可能由少数将来可能接管它的人使用。关键是 - 它不是商业应用程序,也不会公开。我的问题是——在这种情况下,将我的所有类定义为私有而不是公共真的有什么好的论据吗? 整个代码是一个作业序列调度程序。例如,我有一个基本的作业类,如下所示:

class Job {

private:    // should it be?
    int Jobid;

    int stack_row ; // horizontal positon in a stack
    int row_height ;    // position in a column of containers

    float ArrivalTime;
    float FinishTime;
    float GantryTime;

    float WaitingTime; // Job Start - Job Arrival
    float ReachableTime; // later time of Vehicle or YC arrival to job location

    float DueDate;
    float Tardiness; // max{0, Ci-di} Ci, Completion time = Finish time; di, DueDate
    float SlackTime;
    int type; // 1, Loading; 2, Unloading

} ;

然后有一个类用于一系列作业、模拟数据、涉及的机器以及许多其他内容。关键是,我最终得到了很多类,甚至还有更多的组件。所有这些都将仅由我或少数其他人使用。我可以为所有需要设置或读取的私有组件定义所有私有类,使用 set_whatever()get_whatever() 函数,但这真的有意义吗?答 - 这需要时间。 ,它不会产生非常清晰的代码

job_schedule.job_list[i].set_finish_time( job_schedule.job_list[i].get_ArrivaTime() +   job_schedule.job_list[i].get_ProcessingTime() ) ;

B - 当我编写而不是时

job_schedule.job_list[i] = job_schedule.job_list[i].Finish_Time + job_schedule.job_list[i].Processing_Time ;

所以我的问题是 - 在这种情况下我是否有一个真正好的理由坚持使用私有类? 或者也许有一种更优雅的方式来

job_schedule.job_list[i].set_finish_time( job_schedule.job_list[i].get_ArrivaTime() + job_schedule.job_list[i].get_ProcessingTime() ) ;

保持私密性?

I'm facing writing a reasonably large program for a research project that will probably be used only by me, or possibly also by a small number of people that might take it over in the future. The point is - it's not a commercial application, and won't be publicly available. And my question - in such a case are there really any good arguments behind defining all my classes private rather than public?
The whole code is meant to be a job sequence scheduler. So, for example, I have a basic job class that looks like:

class Job {

private:    // should it be?
    int Jobid;

    int stack_row ; // horizontal positon in a stack
    int row_height ;    // position in a column of containers

    float ArrivalTime;
    float FinishTime;
    float GantryTime;

    float WaitingTime; // Job Start - Job Arrival
    float ReachableTime; // later time of Vehicle or YC arrival to job location

    float DueDate;
    float Tardiness; // max{0, Ci-di} Ci, Completion time = Finish time; di, DueDate
    float SlackTime;
    int type; // 1, Loading; 2, Unloading

} ;

Then there's a class for a sequence of jobs, simulation data, machines involved, and a number of other things. The point is, I end up with lot of classes, and even more components for them. All of them will be used either only by me, or a small number of other people. I could define all classes private, with set_whatever(), get_whatever() functions for all private components that need to be set or read, but is there really a point to it? A - it takes time. B - it doesn't make for a very legible code when I write

job_schedule.job_list[i].set_finish_time( job_schedule.job_list[i].get_ArrivaTime() +   job_schedule.job_list[i].get_ProcessingTime() ) ;

instead of

job_schedule.job_list[i] = job_schedule.job_list[i].Finish_Time + job_schedule.job_list[i].Processing_Time ;

So my question is - is there a really good reason I would stick to private classes in this case?
Or maybe there is a more elegant way to do

job_schedule.job_list[i].set_finish_time( job_schedule.job_list[i].get_ArrivaTime() + job_schedule.job_list[i].get_ProcessingTime() ) ;

while remaining private?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

多情癖 2024-12-11 10:13:25

private 不仅仅是依赖管理。

信息隐藏很好,因为它减少了维护,但是如果您只是将类视为数据元组,那么它有什么意义呢?

您应该考虑类的接口,而不是提供一堆 getter/setter 以便任何人都可以操作任何内容,您应该努力定义有意义的方法。

请记住,源代码首先应该是可读的。

There is more to private than dependency management.

Information hiding is good as it lessens maintenance, but what's the point of having a class if you just treat it as a tuple of data ?

You should think about the interface of your class, and instead of providing a bunch of getters/setters so that anybody can just manipulate anything, you should strive to define meaningful methods.

Remember than source code should be readable first and foremost.

岁月如刀 2024-12-11 10:13:25

如果您的程序需要维护一段时间并且不是一次性项目,我强烈建议您使用信息隐藏,即私有成员变量和公共访问器(setter 和 getter)。因为当某些属性发生变化时,您可能永远不知道何时需要添加功能。那时,您将需要一个 setter 函数,它的功能不仅仅是设置成员变量,而且您还必须重构所有以前编写的代码。

所以我想说采用更好的设计。键入有点困难,但典型的代码只键入一次并读取多次。

If your program is meant to be maintained for some time and it's not a one time project I strongly recommend using information hiding, i.e. private member variables and public accessors (setters and getters). Because you may never know when you will want to add a functionality when some propery changes. At that time you will need a setter function that does more than setting the member variable but you'll have to refactor all previously written code.

So I'd say go with the better design. It's a little harder to type but a typical piece of code is typed only once and read many more times.

别想她 2024-12-11 10:13:25

在 C++ 中,没有私有类或公共类之类的东西。您有私有、受保护或公共成员或类。

至于使用继承和封装(信息隐藏)编写大量类 - 我强烈建议您这样做。这将确保您开发的代码更有可能没有错误,并且从长远来看更容易理解、维护和修改。

In C++ there is no such thing as private or public classes. You have private, protected or public members or classes.

As to writing a lot of classes, using inheritance and encapsulation (information hiding) - I would strongly suggest that you do. This will ensure that the code that you develop is more likely to be free from errors and in the long run easier to understand, maintain and modify.

一身骄傲 2024-12-11 10:13:25

为类创建一个接口,但保持数据私有。

为什么不使用 job_schedule.set_finish_time(i, now()); 或类似的方法?

Create an interface for the classes but keep the data private.

Why not job_schedule.set_finish_time(i, now()); or similar?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文