当前位置: 技术问答>java相关
一些关于static modifier 的说明,我有点迷惑,请那位师兄给指点一下!
来源: 互联网 发布时间:2015-03-03
本文导语: With static methods, there is no this. If you try to access an instance variable or call an instance method within a static method, you will get an error message that says,"Undefined variable:this."The concept of "the instance that is executing ...
With static methods, there is no this. If you try to access an instance variable or call an instance method within a static method, you will get an error message that says,"Undefined variable:this."The concept of "the instance that is executing the current method " does not mean anything, because there is no such instance.Like static variables,static methods are not associated with any individual instance of their class.
其实就是说a static method has no this.可是小弟我就是不能理解呀!
请师兄指点一下,最好举例说明一下!定送分!
其实就是说a static method has no this.可是小弟我就是不能理解呀!
请师兄指点一下,最好举例说明一下!定送分!
|
A normal method in a class has a hidden parameter, "this", which identifies which instance the method will operate on. But a static method does not have this hidden parameter, so it can only operate on static members
|
class X{
static m0(){}
m1(){}
}
比方说,你可以用2种方法来使用m0:
- X.m0();
- X x=new X(); x.m0();
但对于m1来说就不同:
- X x=new X(); x.m1();
static m0(){}
m1(){}
}
比方说,你可以用2种方法来使用m0:
- X.m0();
- X x=new X(); x.m0();
但对于m1来说就不同:
- X x=new X(); x.m1();
|
static method 也叫類方法,它可以通過類名直接訪問,也可以通過實例變量來訪問.就像樓上朋友所說的.
static method不能訪問非static變量.
static method不能被override 但是可以被overload
static method不能訪問非static變量.
static method不能被override 但是可以被overload
|
to楼上的:
static method可以被override为static method,但不可以被override为non-static method
原话见:java2认证考试指南(英文原版),第69页。
static method可以被override为static method,但不可以被override为non-static method
原话见:java2认证考试指南(英文原版),第69页。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。