看板 java 關於我們 聯絡資訊
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
mars90226:this 應該是XMLParsingExample吧? 08/05 16:24
兩個物件class為XMLParsingExample,此時this為物件,用以判別是哪個物件. ※ 編輯: b6byc 來自: 122.146.83.112 (08/05 18:23)
a1234957:建議看一下Activity的繼承體系 08/06 00:55
沒錯,Activity繼承了android.content.Context,而XMLParsingExample是屬於父類別.
a1234957:Context才爸爸吧... 08/06 00:57
是的沒錯.
mars90226:那兩句不是在XMLParsingExample的函式中? this就是指自 08/06 10:50
mars90226:己這個類別吧? 08/06 10:54
mars90226:我也覺得二樓說的對 08/06 10:55
this的確是自己這個類別. 初學java,感謝樓上前輩的回答. ※ 編輯: b6byc 來自: 119.77.212.164 (08/06 17:41)