静态成员和非静态成员之间的区别? [英] Difference between static and non static members?

查看:56
本文介绍了静态成员和非静态成员之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
c#中的静态方法是什么?

在对如此多的论坛进行研究后,我决定将自己的问题放在这里:

I found it difficult to clear my mind about the actual concept of static and non-static(instance) members, after researching from so many forums i decided to put my question here:

静态成员和非静态成员有什么区别?

What is the difference between static and non static members?

推荐答案

可以直接从类中访问 static 方法,而 non-static 方法(或实例)我喜欢调用的方法)必须从实例中访问.这就是为什么实例化方法需要实例化,而静态方法则不需要.

The static methods can by accessed directly from the class, while non-static methods (or instance methods as I like to call them) have to be accessed from an instance. That is why instatiating needs to be done for instance methods, while for static methods it's just not needed.

在OOP中,静态变量用于无法由实例变量存储的值.静态方法无法访问类中的实例方法或变量.当然这是有道理的,因为该静态方法不会知道我们要引用的类的哪个实例.

In OOP, static variables are used for values which cannot be stored by an instance variable. static methods cannot access instance methods or variables within a class. Of course that makes sense because that static method would not know which instance of the class we are trying to refer.

例如假设您想统计一个类存在多少个实例?您如何将其存储在单个实例中?

e.g. Supposed you wanted to keep a count of how many instances of a class exists? How would you store that in a single instance?

参考:

  1. 静态方法与非静态方法C#
  2. 静态方法与非静态方法

这篇关于静态成员和非静态成员之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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