【问题标题】:I can not figure out how to let my classes use Information from each other [closed]我不知道如何让我的班级使用彼此的信息[关闭]
【发布时间】:2018-05-08 00:37:54
【问题描述】:

在我的代码中,我试图让 Customer 类使用我创建的地址以及让 Company 类使用我创建的地址,但我无法弄清楚。

class Program
    {
      public static void Main()
        {
            var address = new Address("56 Main St", "Mesa", "AZ", "38574");

        }

    }
    public class Address
    {
        public string Street { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }

        public Address(string Street, string City, string State, string ZipCode)
        {

        }
    }

    public class Customer 
    {
        public long Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public Customer(string FirstName, string LastName)
        {

        }


    }
    public class Company
    {
        public string CompanyName { get; set; }

    }
}

【问题讨论】:

  • 嗨 Dathon,您应该更明确地说明您的问题。你的应用程序崩溃了吗?你有任何错误信息吗?您尝试过什么来解决您的问题?
  • 我正在尝试让这些语句发挥作用。
  • var address = new Address("56 Main St", "Mesa", "AZ", "38574"); var customer = new Customer("John", "Doe", address); var company = new Company("Google", address);
  • 你有一个额外的大括号并且没有设置任何属性/字段。 Tor 不会自动设置字段。您的代码没有做任何有用的事情。查找字段和属性初始化的基础知识。
  • 如果您希望这些语句正常工作,您需要更新 CompanyCustomer 类的构造函数以接受地址,例如public Customer(string FirstName, string LastName, Address address)

标签: c# class


【解决方案1】:

你几乎拥有它。您只需要在构造函数中要求 Address 并使用传入的地址设置相应的属性。

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }

    public Address(string street, string city, string state, string zipCode)
    {
        Street = street;
        City = city;
        State = state;
        ZipCode = zipCode;
    }
}

public class Customer
{
    public long Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }

    public Customer(long id, string firstName, string lastName, Address address)
    {
        Id = id;
        FirstName = firstName;
        LastName = lastName;
        Address = address;
    }


}

public class Company
{
    public string CompanyName { get; set; }
    public Address Address { get; set; }

    public Company(string companyName, Address address)
    {
        CompanyName = companyName;
        Address = address;
    }
}

那么你想要的代码就可以工作了:

var address = new Address("56 Main St", "Mesa", "AZ", "38574");
var customer = new Customer("John", "Doe", address);
var company = new Company("Google", address);

【讨论】:

    【解决方案2】:

    CustomerCompany 类中添加Address 字段,例如public Address MyAddress { get; set; }.

    然后你可以像这样使用它:

    var company = new Company();
    Company.MyAddress = address;
    

    【讨论】:

      猜你喜欢
      • 2014-04-09
      • 2018-05-20
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      • 2020-09-16
      • 2015-09-08
      相关资源
      最近更新 更多