English 中文(简体)
必要价值时采用最佳惯例
原标题:Best Practice for Static vs. Instance Methods when Values are Required

我在这里读到静态和假想的方法,但我看不出回答这个特定问题(可能是绿色的)。

如果您拥有某种特性的等级,以及该类别中需要使用这些特性的一种方法,那么采用静态或假想的方法是更好的?

页: 1

class Foo
{
    //properties
    bar1;
    bar2;

    //method
    sumbar1andbar2()
    {
        return bar1 + bar2;
    }
}

宽巴1andbar2方法需要具备Foo类的特性。 采取静态方法并采用这种方式似乎只是偶然的,因为我人工将该类成员纳入该类方法:

Foo foo1 = new Foo();
foo1.bar1 = x;
foo1.bar2 = y;
sumbar1andbar2(foo1.bar1, foo1.bar2); 

但是,尽管下面的事例方法似乎比较清洁,但我不知道一种清洁和容易的方法,以确保第1条及第2条不作废,这会造成一个例外:

Foo foo1 = new Foo();
foo1.bar1 = x;
foo1.bar2 = y;
sumbar1andbar2();

However, the instance method seems even better if the method modifies another property of the class, like bar3.

最佳回答

First, there is a nice and easy way to ensure that your properties are not null: it is called encapsulation. Make sure the properties are set in the constructor, and do validation in their setter if you choose to expose one. This way the properties will be non-null after the object is constructed (otherwise, constructor would throw an exception), and the property setters would leave property values in a consistent state (otherwise, setters would throw an exception).

接下来是实际问题:如果一种或两种数值都能,则可能 * E/CN.6/2009/1。 Foo使计算方法静态。 否则,它就成为一种实例方法。

P.S. 如果贵方的方法能够收回价值,没有参数,也不会产生副作用,则考虑将其作为计算的财产而不是一种方法。

问题回答

如果该方法的行为有别于“Foo”类(在别处不适用),或者如果它改变“Foo”状态,那么你或许应该把它当作一种诱杀方法。

如果是一般性的计算(如你的例子),你可能想在别处使用这一计算方法,则有几个选择:

使其成为公用事业类别的固定方法,例如

public static class MyUtility {
    public static Int32 Add(Int32 x, Int32 y) { return x + y; }
}

http://msdn.microsoft.com/en-us/library/bb383977.aspx” rel=“nofollow”>extension methods on Foo, 它指父母阶层,或界定x和 y的接口,例如:

// Use as follows:
// var f = new Foo() { x = 5, y = 5 };
// var ten = f.MyUtility();
public static class MyUtility {
    public static Int32 Add(this Foo foo) { return Foo.x + Foo.y; }
}

如果涉及某一具体案件,则该案件必须是审理成员(无论是方法、财产还是外地)。 这些都是最常见的情况,因此,例子很充分。

如果与某个特定案件无关,那么一名审理成员就要求以任何其他方式获得某种使用。 一个很好的例子是Math.Max,如果你打电话Math.Max(43,23),那么结果涉及43大于23,而不是Math的任何财产,这些财产可以设想在申请进行过程中会发生重大变化。

Some classes only have need for static members, so we make the class itself static and it can t be instantiated at all.

由于同样的原因,与类别的性质而不是某一案件有关的诉讼也应当静止不变。 E.g. int.MaxValue is a property of int, not of e.g. 93.

请注意,<代码>int.MaxValue本身就是int。 这并非罕见。 其他例子包括<条码>。 时间Span.Zero和string。 就业:。 这可以是方便的,有时也是在防止重复提及类型(与价值类型无关,在参考类型方面不作过多说明)方面的业绩效益。 重要的是不要这样做。 我们不想在<条码>int上保留4294967296不同的固定财产,使之“大意”叫他们! 一般来说,这在以下情况下都是有益的:

特殊案件可由建筑商建造。

OR:

特别案例通常使用(timeSpan.Zero)和(或)不方便记住(int.MaxValue2147483647或甚至0x7FFFFF更为明确和易于回顾。 当然,这更是这样。

推广方法是一种静态方法,可以称作是偶然成员。 他们非常方便,但在你能够时,通常更能使用一个榜样成员。 当案件成员无法出席时,他们会再次有用,因为:

  1. You don t have access to the source of the class (it s another party s class).
  2. You want to define it on an interface rather than a class.
  3. You want it to be callable on null (avoid, this is non-idiomatic in C# though more common in other languages).
  4. You want to define it for particular cases of generics. For example, if I created MyDictionary<TKey, TValue> that implemented IDictionary<TKey, TValue> I can t define a plus method that adds a number to a stored value because that can only work when TValue is a known numeric type. I can define such a method as an extension method like int Plus<TKey>(this MyDictionary<TKey, int> dict, int addend) that will then appear like an instance member when TValue is int, but won t interfer with the use of MyDictionary for other type parameters.

所有这些案件都给你别无选择,只能使用一种推广方法,但当某审法官做工作时,不会使用。 这一点更清楚,特别是因为有些方面。 互联网的语文只能把一个推广成员视为静态。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...