考慮下面我爲自己的教育寫的代碼。我有一個主要方法,一個靜態方法和兩個類 - 持有和子狀態。子部分延續。通過引用獲取子類的值
class Program
{
static void Main(string[] args)
{
hold h = new hold();
h.aa = 88;
Console.WriteLine("In main " + h.aa);
thismethod(h);
Console.WriteLine("In main2 " + h.aa);
Console.WriteLine("In main3 " + h.ss); //ERROR
Console.ReadKey();
}
static void thismethod (hold h) {
Console.WriteLine("In thismdethod " + h.aa);
h.aa += 1;
Console.WriteLine("In thismdethod1 " + h.aa);
h = null;
subhold subhold = new subhold();
subhold.aa = 8888;
subhold.ss = 22222;
h = subhold;
}
}
class hold
{
public int aa { get; set; }
}
class subhold : hold
{
public int ss { get; set; }
}
我正在試圖訪問h.ss.現在我無法訪問它。我如何從主要方法訪問h.ss
?
幾件事,首先基類不能訪問子類的屬性/成員。第二次閱讀[Jon Skeet在C#中傳遞參數](http://www.yoda.arachsys.com/csharp/parameters.html) – Habib 2014-09-23 21:05:04
'h'是'hold',而不是'subhold'。它沒有'ss'屬性。 – Blorgbeard 2014-09-23 21:07:04
我也嘗試過''這種方法(ref h)'用'static void thidmethod'(ref hold h)'' – Zuzlx 2014-09-23 21:07:32