make class对象有什么好处 [英] what is benefit of make class object

查看:206
本文介绍了make class对象有什么好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我c#中make类的make对象背后的原因是什么。虽然我们可以做同样的工作,使方法成为静态,为什么我们再使用一行来添加(创建对象)。通过创建方法静态,我们也可以使用该方法作为对象

can any one tell me what is reason behind the make object of a class in c#. while we can do the same work to make method as static why we use one more line to add(creating object). By creating method static we can also use the method as making as object

推荐答案

创建和使用对象是 OOP的基本概念。例如,请参阅什么是对象? [ ^ ]。
Creating and using objects is a fundamental concept of OOP. See, for instance What Is an Object?[^].


使用非静态方法允许方法对具有不同属性的类的多个对象进行操作。请参阅 https://msdn.microsoft.com/en-us/library/aa288436( VS.71).aspx [ ^ ]。
Using non-static methods allows the methods to operate on multiple objects of a class all having different properties. See https://msdn.microsoft.com/en-us/library/aa288436(VS.71).aspx[^].


如果方法*可以是静态的,那么将其设置为静态将有助于代码的可用性和性能。您可能遇到的唯一问题是单元测试。如果您需要以某种方式对方法进行存根或模拟,那么您将很难模拟静态方法。如果单元测试对你来说不是问题,或者它是你的方法但你不需要模拟你的方法,那么最好将它们设为静态。
If the method *can* be static then making it static will help with code reasability and performance. The only thing you might have a problem with is unit testing. If you need the method to be stubbed, or mocked in some way then you'll struggle to mock a static method. If unit testing isn't an issue for you, or it is but you don't need to mock your methods then making them static is best.


这篇关于make class对象有什么好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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