首页 > 解决方案 > 提示用户使用给定的构造函数创建 2 个对象的信息

问题描述

我有 3 个课程,保险、员工和保险测试。

Insurance 调用 Employee 类,InsuranceTest 必须通过提示用户输入信息来测试代码,以使用给定的构造函数创建 2 个对象。

保险代码类别:

namespace IT
{
    class Insurance
    {
        private int cust;

        private string agent;
        string state;

        public Insurance (int cust, string agent, string state )
        {
            this.cust = cust;
            this.agent = agent;
            this.state = state;
        }
        public Insurance ( int cust, string agent) 
            : this( cust,  agent, "")
        {
        }
        public int Cust 
        {
            get { return cust; }
            set { cust = value; }
        }

          public string Agent 
        {
            get { return agent; }
            set { agent = value; }
        }
        public string State
        {
            get { return state; }
            set { state = value; }
        }

        Employee e1 = new Employee("Susi", "Insurance Agent Ave SW", 77582);
    }
}

员工类[保险类用信息调用这个类):

namespace IT
{
    class Employee
    {
        string name;
        string address;
        private int id;

        public Employee(string name, string address, int id)
        {
            this.name = name;
            this.address = address;
            this.id = id;
        }
        public Employee(string name, string address) 
            : this( name,  address, 0)
        {
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Address
        {
            get { return address; }
            set { address = value; }
        }
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
    }
}

InsuranceTest 类(我正在努力的代码,它必须提示用户使用构造函数创建 2 个对象的信息[假设员工和保险类] 在显示代理信息、客户数量、当地状态时给出)

namespace IT
{
    class InsuranceTest
    {
        static void Main(string[] args)
        {
            Insurance i1 = new Insurance(5, "Random Insurance", "PA");
        }
    }
}

标签: c#constructor

解决方案


查看评论,您的 SetCustomers 方法不存在。你可以简单地这样做:

i1.Cust = 10; // you can use non zero value instead of 0 to see if Cust is set

要打印 cust,您可以简单地执行以下操作:

Console.WriteLine(i1.Cust);

关于 C# getter 和 setter 以及属性的 MSDN 在这里: https ://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties


推荐阅读