如何在C#静态和非静态方法之间做出选择? [英] How to decide between C# static and non-static methods?

查看:55
本文介绍了如何在C#静态和非静态方法之间做出选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初的问题是为什么要在静态和非静态之间做出选择?两者都做同样的事情……"

My original-question was "Why to decide between static and non-static? Both do the same..."

不幸的是,它被编辑为一个C#特定的问题,我真正想避免的事情.

Unfortunately it was edited to a C#-specific question what I really wanted to avoid.

所以,让我做些补充:

当我说接口时,我不是指C#-keyword-interface,而是我理解的类似C ++-interface的东西:一组定义良好的函数,可与我的对象一起操作. 当说削弱我的界面时,我的意思是我有不同的功能(静态/非静态)执行相同的功能.当有不同的功能来执行相同的操作时,我的界面定义不清.

When I say interface, I don't mean the C#-keyword-interface but what I understand something like a C++-interface: A set of well defined functions to operate with my object. When saying weaken my interface, I mean I have different functions (static/non-static) that do the same thing. My interface is not well defined anymore when there are different functions to do the same thing.

因此,正如看门人鲍勃(Bob)所说,我可以实现Validate()函数

So, as Bob the Janitor posted, I can implement a Validate()-function

Document.Validate(myDocumentObject);    

而且

myConcreteDocumentObject.Validate();

要回到我的Copy()位置,示例可以实现类似的Copy()

To get back to my Copy()-example one could implement Copy() like

myConcreteDocument.Copy(toPath);

而且

Document.Copy(myConcreteDocumentObject, toPath)

Document.Copy(fromPath, toPath)

当我想到一个包含所有属于我的文档的文件的文件夹时(在这种情况下,我不依赖于具体实例,而是依赖于其他事物:)).

when I think of a folder that contains all the files belonging to my Document (in this case I'm not dependent of a concrete instance - but I'm dependent from other things :)).

总的来说,我说的是静态方法,而不是静态类(对不起,如果我忘了提及的话).

In general I'm talking about static methods not static classes (sorry, if I forgot to mension).

但是正如Anton Gogolev所说,我认为我的Document类不是一个很好的例子,并且设计得不好,所以我认为我必须看看单一责任原则".

But as Anton Gogolev said I think my Document class is not a good example and not well designed so I think I will have to have a look at the Single Responsibility Principle.

我还可以实现某种与我的DocumentClass一起使用的ManagerClass:

I could also implement some kind of ManagerClass that operates with my DocumentClass:

例如:

myDocumentManagerObject.Copy(myConcreteDocumentObject, toPath);

myDocumentManagerObject.Copy(myConcreteDocumentObject, toPath);

但是,如果我提到方法1),则我倾向于创建自己执行任务的对象,而不是创建对我的DocumentObject做事情的其他对象(DocumentManager).

but if I refer to approach 1) I would tend to create objects that perform their tasks by themself rather than other objects (DocumentManager) that do something with my DocumentObject.

(我希望这不会成为有关OOP的宗教讨论的方向;).

(I hope this will not take the direction of a religious discussion about OOP ;).)

[/EDIT]

起初,这似乎是一个非常基本的问题,例如何时使用静态方法,何时不使用静态方法",但这是我时不时遇到的问题(而且我很难描述真正的问题是什么;也许只是为了弄清楚为什么(不)使用1)或为什么(不)使用2)).

At first this seems to be a very basic question like "when to use static methods and when not" but this is something I'm confronted every now and then (and I have difficulties to describe what the real problem is; perhaps it's just to get reasons why (not) to use 1) or why (not) to use 2)).

(尽管我使用的是C#语法,但这不是C#限制的问题)

(Although I'm using C#-Syntax this is not a C#-restricted problem)

在OOP中,有两种(以及其他)使用对象的方法:

In OOP there are two approaches (amongst others) of working with objects:

1)如果我希望我的对象做某事,我只是告诉他这样做:

1) If I want my object to do something, I just tell him to do so:

myConcreteObject.DoSomething();

这就像在跟一个对象说话.

It's just like talking to an object.

2)或者,如果您喜欢静态方法:

2) Or if you're a fan of static methods:

ObjectClass.JustDoIt();

在某种程度上,我认为静态函数感觉"更好.因此,我倾向于经常使用静态方法(与具体实例无关-独立永远是一件好事).

In some way I think static functions just "feel" better. So I tend to use static methods very often (to be independent from a concrete instance - independency is always good thing).

因此,在设计课程时,我经常必须决定是采用方法1)还是采用方法2):

So, when designing a class I often have to decide if I take approach 1) or approach 2):

想象一下,您有一个文档"类,该类应代表应保存到数据库中的文档:

Imagine you have a class "Document" which should stand for a document that should be saved into a database:

文档

  • 由文件系统中的一个或多个图像文件组成(这些文件成为单个文档页面)
  • 具有书目之类的字段-用户可以在其中向其添加有关文档的信息-将其保存到额外的文件中
  • 并且应该具有一些操作,例如Copy(),AddPage(),RemovePage()等.

现在,我遇到了创建此类的几种方法:

Now I'm confrontated with several ways to create this class:

//----- 1) non static approach/talking to objects -----
Document newDocument = new Document();

// Copy document to x (another database, for example)
newDocument.Copy(toPath);

我喜欢这样:我告诉文档将自身复制到数据库x中,而对象自己这样做.很好.

I like this: I tell the document to copy itself to database x and the object does so by itself. Nice.

//----- 2) static approach ----------------------------
Document.Copy(myDocumentObject, toPath);

为什么不呢?还不错,感觉很方便...

Why not? Also nice, feels very handy...

那么,实施哪一个?两个都?还是将静态方法应用于某种帮助程序类?还是选择方法1)并坚持使用它而不削弱我的文档类的界面?

So, which one to implement? Both? Or putting the static approach to a kind of helper class? Or choose approach 1) and stick with it to not weaken the interface of my Document-class?

考虑这两种方法时,我得出的结论是(理论上)可以将任何功能实现为静态功能:

When thinking about both approaches I come to the conclusion that (in theory) one could implement any function as a static function:

Class.Function(aConcreteClassObject, parameters);

但也是非静态的:

aConcreteObject.DoSomething(parameters);

举一个真实的例子:

//----- 2) static approach ----------------------------
File.Copy(fromPath, toPath);    // .Net-Framework-like

[/EDIT]

而且:

//----- 1) non static approach ------------------------
ExampeFileClass fileObject = new ExampleFileClass();
fileObject.Copy(toPath);

甚至(某种OOP-Overkill):

or even (kind of OOP-Overkill):

//----- 1) non static approach, too -------------------
fileObject.ToPath = @"C:\Test\file.txt";     // property of fileObject
fileObject.Copy();                           // copy to toPath

那么,为什么(不)使用1)或为什么(不)使用2)?

So, why (not) to use 1) or why (not) to use 2)?

(我不会过多地关注Document类的示例,因为这是关于类设计的一个普遍问题.)

(I would not concentrate on the Document class example too much, since it's more a general question about good class design.)

推荐答案

KISS.如果不必调用构造函数,那就更好了.

KISS. If you don't have to call a constructor, even better.

此外,静态方法应该告诉您有关该函数如何运行的一些信息:

Also, a method being static should tell you a little about how the function operates:

  • 它不会对传递给它的变量之外的变量进行运算.
  • 除了调用该方法时,它不需要任何内存(不计算从函数返回的内容)

还有一些其他重要的事情要注意:

There are some other important things to note:

  • 在某些情况下(Java),静态方法无法被覆盖/子类化,因此它们更适合 实现无需更改 的情况.
  • 有人会认为静态方法是本质上难以测试.

我还将参考此简单的Google搜索,坦率地提供了关于此主题的大量讨论.

I would also refer to this thread, and a simple google search which frankly provides copious amounts of discussion on this very topic.

这篇关于如何在C#静态和非静态方法之间做出选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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