[問題] method 代入 Attributes
請問要如何從 method 去帶入 class 的屬性?
如以下範例
感謝~
class person
{
public string weight = "50";
public string height = "150";
}
class Program
{
// 由 item 可以選擇 weight或是 height
public void printItemValue(person p, string item)
{
Console.WriteLine(p.item);
}
static void Main(string[] args)
{
person A = new person();
person B = new person();
person C = new person();
person D = new person();
List<person> persons = new List<person>() { A , B, C, D};
foreach (person p in persons)
{
printItemValue(p, "weight");
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.217.96.219
※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1541732312.A.333.html
推
11/09 11:10,
6年前
, 1F
11/09 11:10, 1F
→
11/09 12:22,
6年前
, 2F
11/09 12:22, 2F
→
11/09 12:23,
6年前
, 3F
11/09 12:23, 3F
→
11/09 13:24,
6年前
, 4F
11/09 13:24, 4F
→
11/09 14:49,
6年前
, 5F
11/09 14:49, 5F
→
11/09 14:49,
6年前
, 6F
11/09 14:49, 6F
→
11/09 14:49,
6年前
, 7F
11/09 14:49, 7F
推
11/09 19:26,
6年前
, 8F
11/09 19:26, 8F
→
11/09 19:26,
6年前
, 9F
11/09 19:26, 9F
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章