English 中文(简体)
法定方法 1. 用途
原标题:Static method Uses

如果有固定方法 我们只有一份副本才有好处。 无需提及该方法。 也可以做的是制造一个物体,即我们可以使用物体的方法。 为什么我们有固定的方法。 谁能提供解释的实例?

问题回答

当你有私人建筑商时,法定方法就是有益的,因为你想要抽象的即时过程。

例如,C++:

class Foo {
  Foo() {}
public:
  static Foo *create() {
    return new Foo;
  }
};

在这种例子中,抽象的字句只是用其他方式指开的建筑商,但在实践中,你可能希望拥有一组共同的物体,因此,<代码>create()”方法将管理你们。


Sometimes when you have const members which need to be initalised at construction time it can be cleaner to move the logic for this into a private static method, e.g.:

struct Foo;

struct Bar {
  Bar() : f(make()) {
  }

private:
  const Foo f;

  static Foo make() {
    // Create it here
  }
};

The static method is used when developer is really sure the method is only have one instance in the class. There are no other instance that can change that. eg :

  public class People
  {
      private
      public static Int32 GetValue(Int x)
      {
          return x + 3;
      }
  }

So even you are make instances of object people, the return from getvalue static method only produce x + 3. It is usually used when you are really sure to make a functional method like math or physics method. You can refer to functional programming that using static point of view.

一些旧学校的教gu正在过度使用静态方法,而不是采用OOP方法。

eg:

public class People
{
    public static DataSet GetPeopleById(String personId)
    {  .... implementation that using SQL query or stored procedure and return dataset ... }

    public static DataSet GetXXXXXXX(String name, DateTime datex)
    { .... implementation ... }
}

执行上述措施可达数千条线。

This style happens everywhere to make it like OOP style (because it happen in the class) but thinking like procedural approach. This is a help since not all people understand OOP style rather than like OOP style.

The other advantage using static are saving memory footprints and faster. You can see in the blogs : http://www.dotnetperls.com/callvirt





相关问题
Python: Binding method

In following example I am trying to bind a method object via types.MethodType(...). It does not seem to work. Any suggestions? Thanks in advance. import types class Base: def payload(self, *args)...

Invoking an instance method without invoking constructor

Let s say I have the following class which I am not allowed to change: public class C { public C() { CreateSideEffects(); } public void M() { DoSomethingUseful(); } } and I have to call M ...

Behaviour of final static method

I have been playing around with modifiers with static method and came across a weird behaviour. As we know, static methods cannot be overridden, as they are associated with class rather than instance....

Setting the Page-Title in .Net using C# from a Class

Question, How do i set the title of a page from a class. Is it even possible? I can and have set the page title from a page itself and a usercontrol. Can I, How Do I do this via a class using C# ....

how to drag in the animation in iphone application?

Iphone application is using the static co-ordinates to reach at some points in the application,which is based on the button event. But what i want is when i drag the item than it should reach at some ...

热门标签