iT邦幫忙

0

C# 類別 物件 問題

c#

抱歉 請問 如何讓 commodity 去替代掉 shop類別裡面的Commodity屬性呢
https://ithelp.ithome.com.tw/upload/images/20210228/20135541qNSX2JyGPR.png

tunin iT邦新手 4 級 ‧ 2021-03-02 14:28:15 檢舉
不太懂你的需求,你說的"替代掉"是要替代成什麼?
Homura iT邦高手 1 級 ‧ 2021-03-02 15:03:03 檢舉
shop裡的commodity 不是右邊commodity ?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
Homura
iT邦高手 1 級 ‧ 2021-03-02 15:29:57

我沒猜錯你應該是想動態換掉shop內commodity的型態吧
我知道2種方法
一種是使用object這個基底類別
只要是物件就能裝進去

class shop
{
  public List<Object> commodity {get;set;}
}

第二種是使用泛型
在new時候決定型態

class shop<T>
{
  public List<T> commodity {get;set;}
}

我要發表回答

立即登入回答