[問題] this的使用..
請教一下
最近突然想到this這個問題,試著寫一個小小的程式片段..
發現,我好像沒搞懂...
我的理解是,因為欄位和參數有相同的名稱
參數會把欄位蓋掉
所以要用this,是說我要用這個欄位,而不要參數
然後我就寫了一小段
class thistest
{
public thistest (int testvalue)
{
this.testvalue = testvalue;
MessageBox.Show("testvalue is "+ testvalue);
}
private int testvalue = 20;
}
//==================
private void button1_Click(object sender, EventArgs e)
{
thistest value = new thistest(10);
}
我的想法是說,按下按鈕,傳10進去
用this的話就是10
不用的話就是20
結果...好像加不加this都是10...
然後我另外還有想到一個是..為什麼參數和欄位名稱會是一樣的?
既然會蓋過去,不能設不一樣的就好了嗎?
麻煩前輩教導一下了
謝謝幫忙
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.42.112.154
※ 編輯: bernachom 來自: 114.42.112.154 (05/18 00:47)
推
05/18 02:03, , 1F
05/18 02:03, 1F
→
05/18 02:04, , 2F
05/18 02:04, 2F
→
05/18 02:12, , 3F
05/18 02:12, 3F
→
05/18 02:12, , 4F
05/18 02:12, 4F
→
05/18 02:13, , 5F
05/18 02:13, 5F
→
05/18 02:14, , 6F
05/18 02:14, 6F
→
05/18 08:28, , 7F
05/18 08:28, 7F
→
05/18 08:29, , 8F
05/18 08:29, 8F
→
05/18 13:45, , 9F
05/18 13:45, 9F
推
05/21 10:00, , 10F
05/21 10:00, 10F
討論串 (同標題文章)