本帖最后由 李建强 于 2012-9-24 19:45 编辑
- class TestFu
- {
- public void show()
- {
- System.out.println("我是Test的父类!");
- }
- }
- public class Test extends TestFu
- {
- public void show()
- {
- System.out.println("我就是传说中的Test了。");
- }
- public void callOverridedMethod()
- {
- Object obj = this;
- System.out.println(obj.toString());//this是引用。
- super.show();//这一句正常
- //super不是引用。如果super是引用的话,那么为什么不可以把引用传给obj1?
- //下面这句不能编译。
- Object obj1 = super;
- }
- public static void main(String[] args)
- {
- Test t=new Test();
- t.show();
- t.callOverridedMethod();
- }
- }
复制代码 对于super,
《The Java Programming Language》上这样说:
In field access and method invocation, super acts as a reference to the current object as an instance of its superclass.
这里面super acts as a reference ,说super 只是acts as,但不是reference。如果不是引用是什么?
对于this.
JLS上这样说:
When used as a primary expression,
the keyword this denotes a value that is a reference to the object for which the instance method was invoked (§15.12),
or to the object being constructed.
这里说this is a reference。说this是一个reference。
这样理解可对?
还有,怎么使用它们就不要说了。 |