静态和非静态方法 [英] static and non-static method

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

问题描述

为什么我们不能使用具有相同名称的静态和非静态方法...?

这背后的原因是什么,即使我们使用不同的调用机制作为



class_name.static_method();









instance.non-static_method();

Why we can't have a static and a non-static method with same name...?
Whats the reason behind this even we use different calling mechanism for both as

class_name.static_method();

and


instance.non-static_method();

推荐答案

看看这个.. static-and-instance-methods-with-the-same-name [ ^ ]
have a look at this..static-and-instance-methods-with-the-same-name[^]




静态方法属于类本身,非静态(或实例)方法属于从该类生成的每个对象。如果您的方法执行的操作不依赖于其类的各个特征,请将其设置为静态(这将使程序的占用空间更小)。否则,它应该是非静态的。



您可以调用这样的静态方法:
Hi,
A static method belongs to the class itself and a non-static (or instance) method belongs to each object that is generated from that class. If your method does something that doesn't depend on the individual characteristics of its class, make it static (it will make the program's footprint smaller). Otherwise, it should be non-static.

You can call static methods like this:
Foo.method1()

。如果您尝试使用method2,它将失败。但这样可行:

. If you try that with method2, it will fail. But this will work:

Foo bar = new Foo(1);

bar.method2();


HI ..

访问此链接

http://www.c-sharpcorner.com/uploadfile/abhikumarvatsa/static-and-non-static-methods-in-C-Sharp/ [ ^ ]
HI..
visit this link
http://www.c-sharpcorner.com/uploadfile/abhikumarvatsa/static-and-non-static-methods-in-C-Sharp/[^]


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

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