///<summary> /// 判断是否成年 ///</summary> ///<returns></returns> publicboolIsAdult() { return age >= 18; } ///<summary> /// 添加朋友的方法 ///</summary> ///<param name="p">传入的新朋友</param> publicvoidAppendFriend(Person p) { if (friends == null) { friends = new Person[] { p }; } else { // 扩容 Person[] newfriends = new Person[friends.Length + 1]; for (int i = 0; i < friends.Length; i++) { newfriends[i] = friends[i]; } newfriends[friends.Length - 1] = p; friends = newfriends; } } }
internalclassProgram { staticvoidMain(string[] args) { Person p = new Person(); p.name = "MrTang"; p.Speak("I Love C#"); p.age = 18; if (p.IsAdult()) { p.Speak("我成年了"); } Person p2 = new Person(); p2.name = "小明"; p2.age = 16;
p.AppendFriend(p2);
for (int i = 0; i < p.friends.Length; i++) { WriteLine(p.friends[i].name); } } }