![](/img/trans.png)
[英]How to send Class type to another class and create instance of this class?
[英]How do I create an instance of a class that has a property with the type of another class?
有人可以溫柔地向我解釋如何創建Person
class 的實例,我很難做到。 它們來自不同的檔案,我只是將它們放在一起以便更容易查看。
public class Person
{
protected int id;
public string Name { get; set; }
public long Cpf { get; set; }
public Telephone[] Phones { get; set; }
}
public class Telephone
{
protected int id;
public int Number { get; set; }
public int Ddd { get; set; }
}
public static void Main(string[] args)
{
Person p = new Person();
p.Phones[0] = ??;
}
您可以像創建其他實例一樣創建實例,就像您所做的那樣:
Person p = new Person();
然后,您必須為數組分配大小並對其進行實例化(我建議在人員 class 中的某處執行此操作,但可以從任何地方完成):
p.Phones = new Telephone[10];
電話陣列現在有 10 個可用於電話實例的空間。 如果您想像示例中那樣訪問索引 0,則必須先添加它:
p.Phones[0] = new Telephone();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.