[問題] this 的疑問
import android.widget.LinearLayout;
public class XMLParsingExample extends Activity{
略...
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
略...
}
new LinearLayout(this)中的this 應該是屬於object,送給LinearLayout的建構子
但是建構子的型態是 LinearLayout(Context context)
Context的型態 public abstract class Context extends Object
這樣不就是父類別(this)轉成子類別(Context xontext),不是需要作強迫轉型嗎?
所以 LinearLayout layout = new LinearLayout((Context)this);
但是eclipse報錯,所以想請教一下,我的觀念哪裡錯誤呢?
感謝...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.146.83.112
推
08/05 16:24, , 1F
08/05 16:24, 1F
兩個物件class為XMLParsingExample,此時this為物件,用以判別是哪個物件.
※ 編輯: b6byc 來自: 122.146.83.112 (08/05 18:23)
推
08/06 00:55, , 2F
08/06 00:55, 2F
沒錯,Activity繼承了android.content.Context,而XMLParsingExample是屬於父類別.
推
08/06 00:57, , 3F
08/06 00:57, 3F
是的沒錯.
推
08/06 10:50, , 4F
08/06 10:50, 4F
→
08/06 10:54, , 5F
08/06 10:54, 5F
→
08/06 10:55, , 6F
08/06 10:55, 6F
this的確是自己這個類別.
初學java,感謝樓上前輩的回答.
※ 編輯: b6byc 來自: 119.77.212.164 (08/06 17:41)
java 近期熱門文章
3
14
PTT數位生活區 即時熱門文章