静态函数的用法,重要性 [英] static functions usage, importance

查看:65
本文介绍了静态函数的用法,重要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么需要静态函数?

推荐答案


中的好解释
http://stackoverflow.com/questions/558122/what-is-a-static-function





http:// stackoverflow .com / questions / 4921150 / when-to-use-static-member-function


static = not(dynamic)<br />
not(dynamic) = no instance specific<br />
no instance specific = class specific<br />
<br />
therefore, static = shared (run-time sharing of variables/objects between instances of a particular class.)





并且访问那些静态变量/对象我们需要静态方法/函数...



静态变量/对象是类特定而非实例-s非常...

这样,

静态变量&方法可以直接使用classname 调用。




实时示例

你想要计算特定类创建的对象数量

然后在创建该类的新实例时声明静态变量和递增 - 递减值。处理该实例



快乐编码!

:)



And for access those static variables/objects We require Static methods/Functions...

static variables/objects are class specific not instance-specific...
so that,
static variables & methods can directly call using classname.


real time example
you want to count how many objects created of particular class
then declare static variable and increment-decrements value when you are create new instance of that class & dispose that instance

Happy Coding!
:)


静态函数不需要实例调用此方法。在编程中我们通常使用静态方法,当我们需要在不同的类,不同的模块中调用相同的操作时。



例如:Conrol验证必须在不同的UI类中执行。



inorder写相同的逻辑一次又一次你可以定义一个静态方法



Static function does not need an instance to call this method. In programming we normally use static method when we need same operation has to be called in different classes, different modules.

eg: Conrol validation has to be performed in diffrent UI class.

inorder write the same logic again and again you can define a static method

class Validator
{
  static bool Validate(TextBox text)
  {
    bool success = false;

    if(!string.IsNullOrEmpty(text.Text))
    {
      success = false;
    }
   return success;
  }
}

so you can call this method as 
Validator.Validate(myTextBox);
Validator.Validate(mycomboBox); // this is an example



so你不需要创建一个实例来调用这个方法。静态方法非常方便。

您可以从谷歌或代码项目中找到更多慷慨的答案。我让你的理解非常简单。


so you dont need to create an instance to call this method. Static methods are very handy.
You can find more generous answer from google or code project. I made it very simple for yours understanding.


这篇关于静态函数的用法,重要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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