//something is wrong with Parents setter property idk what :C,
//i'm getting an exception when third ctor executes...
class Person {
public string Name { get;set; }
public DateTime DateOfBirth { get; private set; }
public IEnumerable<Person> Parents { get { return (Person[])Parents; }
private set { Parents = value; } } // I'm getting StackOverFlow here BUT WHY ?
public Person(string Name,DateTime DateOfBirth,Person[] parents=null)
{
this.Name = Name;this.DateOfBirth = DateOfBirth;Parents = parents;
}
}
// Main class
class Program
{
static void Main(string[] args)
{
//Creating 3 objects and get exception on third
Person prs = new Person("Emily", DateTime.Now, null);
Person prss = new Person("mark", DateTime.Now);
Person psrs = new Person("maras", DateTime.Now, new[] { prs, prss });
if (psrs.Parents != null)
{
foreach (var parent in psrs.Parents)
Console.WriteLine();
}
else Console.WriteLine("No parents");
}
}
//問題另外:我的類實現是否符合OOP的所有原則? //好吧也許這是因爲在構造函數和屬性我做的事情完全一樣,但如果我會刪除Parent =父母在ctor我怎麼可以通過構造函數初始化我的父母屬性?如果我刪除我的集合propery我不能初始化父母的財產。請幫助 !StackOverFlow異常我不明白爲什麼?
'Parents'是自參照。 – crashmstr
請看[這個問題](http://stackoverflow.com/questions/36344909/stackoverflow-exception-i-dont-get-why) –
帖子不包括[MCVE] - 顯示的代碼不是最小的。 (除了詢問已經問及已經回答了很多時間的問題)。 –