创建新类的利弊是什么? [英] What are the pros and cons of creating a new class?

查看:53
本文介绍了创建新类的利弊是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常基本的问题,但这是我实际遇到的一个问题,因为我正在学习更多关于 Actionscript 3 的内容.但是,我的第一个问题很笼统:什么时候将功能放在一个新类中而不是同一个类中的新函数中?根据这个Java教程,它侧重于基本的面向对象原则上,类应该是对象的蓝图".我一直认为这意味着对象将使用的任何功能或行为都应该包含在类中.然而,根据单一责任原则,每个类应该只有一个改变的理由.例如,您应该有一个类来编译报告和一个类来打印它,而不是一个 Report 类.

This is a probably a very basic question, but it's one I'm actually running into as I'm learning more about Actionscript 3 in particular. However, my first question is very general: When is appropriate to put functionality in a new class rather than a new function in the same class? According to this Java tutorial, which focuses on basic object-oriented principles, a class is supposed to be a "blueprint of an object". I always understood this to mean that any functionality or behavior that the object would use should be contained within the class. However, according to the single responsibility principle, each class should have only one reason to change. For example, you should have one class to compile a report and one class to print it rather than a single Report class.

你们能帮我了解创建新课程的利弊吗?将一个对象拆分为多个类的成本是多少?将相关功能保留在同一个类中或将其拆分为两个类是否存在编译时间或性能成本?是否有时您想将事情分开,而其他时候您可能想将它们放在一起?

Can you guys help me understand the pros and cons to creating a new class? What are the costs to splitting an object into multiple classes? Are there compile-time or performance costs for keeping related functionality in the same class, or for splitting it into two? Are there perhaps times that you would want to split things out, while you might want to keep them together other times?

推荐答案

据我所知,拥有 1 个可以做任何事情的类或几个可以做任何事情的类并没有区别做同样的事情.

As far as I remember, there isn't a big difference between having 1 class which can do everything or several classes which can do the same.

这是关于可读性以及如何扩展代码.这也只是关于干净的代码和耦合.

It's about readability and how you can extend the code. It's also just about clean code and coupling.

如果您有一个名为Printer"的类,您不希望其中包含WaterCoolerSound()".当然,您拥有的对象越多,内存不足的可能性就越大.但我不完全确定是一个具有所有功能的对象还是几个具有相同功能的类分散开,会占用更多内存.

If you have a class called "Printer" you don't want to have "WaterCoolerSound()" in it. Of course the more objects you have the higher the chance is that you can run out of memory. But I am not entirely sure whether one object with all functionality or several classes with the same functionality spread out, takes more memory.

事实上,您可以说,如果您只需要一个小包来保存一些数据,而不能同时像熊一样跳舞,那么拥有两个独立的类是有意义的.

In fact, you could say that if you JUST need a little bag to hold on to some data and not be able to dance like a bear at the same time, it would make sense to have two separate classes.

建议在拥有代码之前不要考虑性能.从可维护性和可理解性的角度来看,当然,更小的类和更小的方法更胜一筹.(再次参见单一职责原则:)

It's advisable not to think about the performance before you have the code. From the maintainability and understandability viewpoint, of course, smaller classes, with smaller methods are superior. (see The Single Responsibility Principle again :)

这篇关于创建新类的利弊是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆