0
我试图更好地理解类,类'地址'被用作'Person'类属性'ShippingAddress'中的类型,但我想知道如何将值分配给地址属性,因为它下面的方式是给我一个错误:如何将一个类用作另一个类中的属性,然后实例化它? [C#]
**型“System.NullReferenceException”未处理的异常发生在Classes_and_Objects.exe
其他信息:对象未将参考设置为对象的实例。**
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Classes_and_Objects
{
class Program
{
static void Main(string[] args)
{
Person john = new Person();
john.FirstName = "John";
john.LastName = "Doe";
john.ShippingAddress.StreetAddress = "78 Fake Street" ;
john.ShippingAddress.City = "Queens";
john.ShippingAddress.State = "NY";
john.ShippingAddress.PostalCode = "345643";
john.ShippingAddress.Country = "United States";
}
}
public class Address
{
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Address ShippingAddress { get; set; }
}
}
}