组织面向对象代码的最佳方式是什么?

发布于 2024-09-05 19:09:47 字数 122 浏览 10 评论 0原文

我已经很长时间没有用 java 编码了,在用 C 编码之后,我开始为 OOP 组织我的代码。更具体地说,我不确定何时创建新方法,何时创建新类,以及何时将所有内容集中在一起。

是否有一些关于如何完成的一般规则或指南?

I haven't coded in java for a long time, and after coding in C, I'm having issued organizing my code for OOP. More specifically I'm not sure when to create a new method, and when to create a new class, and when to just lump everything together.

Are there some general rules or guidelines on how it should be done?

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

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

发布评论

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

评论(4

青巷忧颜 2024-09-12 19:09:47

查看SOLID 原则

编辑(更多提示):

您需要SOLID 掌握一些设计原则。

要从小事做起,请先查看以下内容:

编写代码时,高 可维护性应该是您的最终目标,这一切都与分配责任和关注点分离有关。

Take a look at the SOLID principles.

EDIT (some more pointers):

You need a SOLID GRASP of some design principles.

To start small, take a look at these first:

When writing code, high maintainability should be your ultimate goal, and it's all about assigning responsibilities and separation of concerns.

可爱暴击 2024-09-12 19:09:47

首先,永远不要把所有事情都混在一起。
首先尝试识别对象。为程序将使用的每个对象构建一个类。如果您正在为卡车司机构建一个应用程序,您将需要一个针对驾驶员、卡车、他所拖运的货物的类,您可以将这些较大的物体分解到多远,实际上没有限制。至于方法,方法处理对象的操作。 Truck.Start() 将启动卡车。 Drive() 会开始驾驶,等等...也许 Drive 方法采用 Route 对象作为参数,其中包含要行驶的道路。简而言之,当一个对象需要做某事时创建一个方法,当你想要处理另一种类型的对象时创建一个类。

First of all, never just lump everything together.
Try to identify the objects first. Build a class for each object your program will work with. If you're building an application for truck drivers, you will need a class for the driver, the truck, the load he's hauling, there's really no limit to how far you can break these bigger objects down. As for the methods, a method handles an action for the object. Truck.Start() would start the truck. Drive() would start it driving, etc... Maybe the Drive method takes a Route object for an argument which contains the roads to drive on. In short, create a method when an object needs to do something and create a class when you want to deal with another type of object.

数理化全能战士 2024-09-12 19:09:47

设计良好的对象是SOLID

这是一个难题,每个关心设计的人都必须弄清楚。我认为这是使用对象最难的部分,远远超出了单纯的语法。

Well designed objects are SOLID.

This is a difficult question, one that everyone who worries about design has to figure out. I think it's the hardest part of using objects, light years beyond mere syntax.

怪我入戏太深 2024-09-12 19:09:47

它是您认为系统中的事物、实体或参与者的东西。那么它是一个对象,所以创建一个类来表示它。没有必要让它变得更加困难。

Is is something that you think of as a thing, entity or actor in your system. Well then it's an object so make a class to represent it. There's no need to make it more difficult than it is.

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