作者AI3767 (星泉幼子)
看板java
标题Re: [问题] 一个 java expression 里有两个 this
时间Sun Mar 29 11:23:46 2009
※ 引述《Fenikso (ばかちーは俺の嫁)》之铭言:
: (删光光)
: class A {
public static void main(String[] args) { new A().new C(); }
A() { System.out.println("A:constructor"); }
: A getA() {
System.out.println("A:getA()");
return this;
}
: class B {
B() { System.out.println("B:constructor"); }
B(int v) { System.out.println("B:constructor (int)"); }
}
: class C extends B {
: C() {
: A.this.getA().super(2);
getA();
A.this.getA();
: }
void getA() { System.out.println("C:getA()"); }
: }
: }
: 两个this和两个super的我凑不出来orz
帮你加几个东西进去, 比较好了解 ^^>
这个例子应该可以多加探讨
A.this.getA() 与 getA() 有什麽不同可以由此得知
为了在 C 内可以使用 A 的 getA(),
利用 A.this 指定是在 "A的这个" 之下 的 getA()
A.this.getA().super() 或 A.this.getA().super(2)
可以得知是去用了 B (也就是 C 的super class) 的建构式
且分别与
A.this.super() 以及 A.this.super(2)
同义
当然更同义於
super() 以及 super(2)
只是我不明白语意 ><
A.this.getA() 回传的是 A 型态, 怎麽使用super()时
意义上却是 B 型态的建构式?
是否有个例子可以说明, 一定要使用 A.this.super() 的用法?
(就像是 A.this.getA()与 getA() 这样的差异)
--
不过, 以前还真没想到过要同时使用 this 和 super 的 XD
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 122.146.194.91