为什么使用类而不是函数? [英] Why use classes instead of functions?

查看:268
本文介绍了为什么使用类而不是函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一些类的优点,例如变量和函数作用域,但除此之外,对于我来说,有更多的功能组似乎更容易,而不是拥有许多类的实例和抽象。那么为什么规范将一个类中的类似函数分组?

解决方案

OOP的目的不是类似在课堂上的功能'。如果这是你所做的一切,那么你不会做OOP(尽管使用OO语言)。有类而不只是一堆函数有你提到的'变量和函数作用域'的副作用,但我认为它只是一个副作用。



OOP是关于封装,继承,多态,抽象等许多概念。它是软件设计的一种特定方式,是将问题映射到软件解决方案的一种具体方式。

I do know some advantages to classes such as variable and function scopes, but other than that is just seems easier to me to have groups of functions rather than to have many instances and abstractions of classes. So why is the "norm" to group similar functions in a class?

解决方案

The point of OOP is not to 'group similar functions in a class'. If this is all you're doing then you're not doing OOP (despite using an OO language). Having classes instead of just a bunch of functions has a side effect of 'variable and function scopes' that you mention, but I see it just as a side effect.

OOP is about such concepts as encapsulation, inheritance, polymorphism, abstraction and many others. It is a specific way of software design, a specific way of mapping a problem to a software solution.

这篇关于为什么使用类而不是函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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