1 class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Person person = new Person("001", "Allan", "", 23, "软件工程师");
 6             Person person2 = new Person("002", "Angelina", "", 23, "销售经理");
 7             Console.WriteLine("{0}, {1}, {2}, {3}, {4}", person.ID, person.Name, person.Gender, person.Age, person.post.Name);
 8             Console.WriteLine("{0}, {1}, {2}, {3}, {4}", person2.ID, person2.Name, person2.Gender, person2.Age, person2.post.Name);
 9             Console.ReadKey();
10         }
11     }
12 
13     class Person
14     {
15         public Person() { }
16         public Person(string id, string name, string gender, int age, string postName)
17         {
18             ID = id;
19             Name = name;
20             Gender = gender;
21             Age = age;
22             post.Name = postName;
23         }
24         public string ID { get; set; }
25         public string Name { get; set; }
26         public string Gender { get; set; }
27         public int Age { get; set; }
28 
29         private static  Post _post = null; 
30         public Post post
31         {
32             get 
33             {
34                 if (_post == null)
35                     _post = new Post();
36                 return _post;
37             }
38         }
39     }
40 
41     /// <summary>
42     /// 职务
43     /// </summary>
44     class Post
45     {
46         public string ID { get; set; }
47         public string Name { get; set; }
48     }
View Code

相关文章: