我应该把一切都变成静态还是非静态的? [英] Should I make everything static or non-static?

查看:78
本文介绍了我应该把一切都变成静态还是非静态的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我是初学者,据我所知,Code Entry Point必须是静态的。但是你只能在theese静态Main类中引用静态变量和静态方法。



我还没有进行大型多线程,所以我基本上把所有东西都做成了静态。现在我开始进行单元测试,似乎静态变量和静态方法在单元测试的**中很难。



你推荐什么?我,我应该把所有东西都做成非静态的并参考吗?



我有点迷失在这里。



谢谢你的帮助。



真诚地:

~Jonas

解决方案

尽可能少的应该是静态的 - 除了必须的扩展方法,以及一些对应用程序通用而不是特定于类的方法。



C#是一种OOP语言 - 第一个O是对象!如果你把所有东西都当作静态,那么你就会忽略使语言起作用的原因。
这只是一种看待事物的不同方式:而不是像在中那样重要的过程。传统的应用程序,对象变得能够处理自身,因此处理同一类型对象的多个实例要容易得多。



想想一位母亲有六个孩子:如果她要按时上学,如果孩子们知道如何打扮自己,这一切都会好起来,所以母亲可以继续做其他事情,比如整理早餐。所以Dress成为了Child类的一种方法,母亲只是发出指示:

 mike.Dress(); 
joe.Dress();
sandra.Dress();
...



  foreach (儿童< span class =code-keyword> in  children)
{
child.Dress();
}



而不是让母亲知道怎么做:

连衣裙(麦克); 
连衣裙(乔);
...

在OOPs(非静态)版本中,孩子知道如何打扮自己(你可以为BoyChild和GirlChild使用不同的方法 - 两者都来自Child - 在女孩身上穿上一件衣服,在男孩身上穿裤子。)

在较旧的传统方法中,母亲知道如何给孩子穿衣,并且必须检查她的代码是什么她为穿上合适的衣服打扮的孩子的类型。



这一切都有很多,但这是关于它的一个小的测试盒! :笑:


Hi guys.

I'm a beginner, and as far as I know the Code Entry Point has to be static. But you can only reference static variables and static methods within theese static Main class.

I didnt made big multithreading yet, so I basically made absolutely everything static. Now I'm starting with Unit testing and it seems like that static variables and static methods are a pain in the a** for unit testing.

What do you recommend me, should I make everything non-static and referende it?

I'm kind of lost here.

Thanks, for all your help.

Sincerely:
~Jonas

解决方案

As little as possible should be static - excepting extension methods, which have to be, and some methods which are "generic" to an application rather than specific to a class.

C# is an OOPs language - and the first "O" is for "Object"! If you make everything static, then you are ignoring what makes the language work in the first place.
It's just a different way of looking at things: instead of the procedure being the important thing as in a "traditional" application, the object becomes capable of dealing with itself, and thus it's a lot easier to handle multiple instances of the same type of object.

Think of a mother with half a dozen children: if she is to get them all to school on time, it all works better if the children know how to dress themselves, so the mother can get on with other things, like sorting out breakfast. So Dress becomes a method of the class Child, and the mother just issues instructions:

mike.Dress();
joe.Dress();
sandra.Dress();
...

Or

foreach (Child child in children)
   {
   child.Dress();
   }


Instead of having to have the mother work out how to do it:

Dress(mike);
Dress(joe);
...

In the OOPs (non-static) version, the child knows how to dress itself (and you can have different methods for BoyChild and GirlChild - both of which are derived from Child - to put a dress on the girls, and trousers on the boys for example).
In the older "traditional" method, the mother knows how to dress a child and has to check in her code what type of child she is dressing to get the right clothes on.

There is a heck of a lot more to it all that this, but that's about it for a small test box! :laugh:


这篇关于我应该把一切都变成静态还是非静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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