具有此关键字的实例和静态方法 [英] Instance and Static Method with this Keyword
本文介绍了具有此关键字的实例和静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是学生。我有一个问题!。我不明白这个关键字的实例和静态方法..请帮助我。谢谢你!
解决方案
简而言之:实例方法获取隐式参数this,用于访问完成调用的实例。静态方法没有它;它大致相当于前OOP功能/程序;在.NET中,静态方法不能是虚拟的(但也有技术可以)。
为了说明实例方法机制,请考虑这个:
class MyClass {
internal void SomeInstanceMethod(){ // this参数未显示,但它就在那里
这个 .SomeMember = 3 ; // SomeMember对象是哪个?见下文......
}
内部 int SomeMember { get ; set ; }
}
MyClass myInstance = new MyClass();
myInstance.SomeInstanceMethod();
// 在此次调用中输入SomeInstanceMethod堆栈帧时,
// 这等于myInstance
// 如果是静态方法,则调用等同于
// < span class =code-comment> SomeInstanceMethod(myInstance)
如你所见,myInstance。*()
call是将实例参数this传递给方法的方法。
-SA
i am student .i have a question!.i dont understand "Instance and Static Method with this Keyword"..plz help me .thank you!
解决方案
In brief: instance method gets implicit parameter "this" used to access the instance on which the call is done. Static method does not have it; it is roughly equivalent to the pre-OOP function/procedure; in .NET, a static method cannot be virtual (but there are technologies where it can be).
To illustrate instance method mechanism, consider this:
class MyClass { internal void SomeInstanceMethod() { // "this" parameter is not shown, but it's there this.SomeMember = 3; // which SomeMember object is this? see below... } internal int SomeMember { get; set; } } MyClass myInstance = new MyClass(); myInstance.SomeInstanceMethod(); // when you enter SomeInstanceMethod stack frame in this call, // this equals to myInstance // if it was a static method, the call would be equivalent to // SomeInstanceMethod(myInstance)
As you can see,myInstance.*()
call is the way to pass instance parameter "this" to the method.
—SA
这篇关于具有此关键字的实例和静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文