c# - 提示用户使用给定的构造函数创建 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");
}
}
}
解决方案
查看评论,您的 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
推荐阅读
- python - 为什么 Python Selenium 下载 0 KB 文件?
- python - 使用 PulP 的优化问题 - 目标函数中的误差
- reactjs - 是否可以忽略缺少的 NPM(对等)依赖项?
- reactjs - Why is Typescript giving "Duplicate Identifier" error
- c++ - 查找向量中仅重复一次的值
- python - 如何避免 SWIG 的“不推荐使用的命令行选项:-modern。此选项现在始终处于启用状态。”
- javascript - Materialise Sidenav 不使用 React
- javascript - JS 中的滑动和/或动画问题
- google-chrome-extension - EditThisCookie 如何从 Chrome 编辑 Local HttpOnly Cookie 的值?
- php - Wordpress 在主页上显示博客片段/链接的特色图片