\\今天在看.Net3.0的新特点,看到有个对象初始化器,但是在实际使用时不知道怎么实例化。。。高手帮忙看看。。谢谢了!
using System;
class Person
{
public string Name { get;set;}//大括号后不加;
public string Sex { get; set; }
public int Age { get; set; }
public Person(int age)
{
Age = age;
}
public class Student
{
public string School { get; set; }
}
}
class s
{
static void Main()
{
//这下边这两句我不知道怎么写了。。请高手帮忙。。
Person p = new Person(22) { Name="yy",Sex="Man",Student=new Student{School="jiuCheng"}};
Console.WriteLine("Name:"+p.Name+"Sex:"+p.Sex+"Age:"+p.Age+"School"+Person.Student.School);
}
}
class Person
{
public string Name { get;set;}//大括号后不加;
public string Sex { get; set; }
public int Age { get; set; }
public Student student { get; set; }
public Person(int age)
{
Age = age;
}
public class Student
{
public string School { get; set; }
}
}
class s
{
static void Main()
{
//这下边这两句我不知道怎么写了。。请高手帮忙。。
Person p = new Person(22) { Name = "yy", Sex = "Man", student =new Person.Student { School = "jiuCheng" } };
Console.WriteLine("Name:" + p.Name + "Sex:" + p.Sex + "Age:" + p.Age + "School" + p.student.School);
}
}
哪儿不会啊,代码中是正确的啊
代码没有问题啊。
public class Student
{
public string School { get; set; }
}
是Student类型声明,并不是不是Person的字段。
楼上的SNAIL_ll已经帮忙把修正后的code贴出来了,参考SNAIL_ll的code就好了。