C# struct

来源:百度知道 编辑:UC知道 时间:2024/04/29 17:51:46
struct Person
{
public string Description
{
get;
set;
}
public int Age
{
get;
set;
}
}
class Company
{
public Person People
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
//Machine nn = new SubMachine();
//nn.Print();
//Thread.Sleep(5000);
Company company = new Company();
company.People.Description = "people";
company.People.Age = 78;
Console.WriteLine(company.People.Age);
Console.WriteLine(company.People.Description);
string str = Console.ReadLine();
while (str != "quit")

有前途阿 还用的日文版的
你的属性都没有具体写东西啊,比如
class Company
{
private Person _people
public Person People
{
get{ return _people; }
set{ this._people = value;}
}

public Company()
{
this._people=new Person();
}
public Company(Person person)
{
this._people=person;
}

}

你只是初始化了Company,People并没有被初始化,所以直接
company.People.Description = "people"; 会报错,
最好在Company的构造函数中初始化一个Person