11

我有一个叫做雇员的类,它有一个叫做保险的字段,它是这样的保险类型

public class Employee
{
    public string Name;
    public Insurance Insurance;
}

我还有一门叫保险的课

public class Insurance
{
    public int PolicyId;
    public String PolicyName;
} 

现在在主程序中我想做类似的事情

var myEmployee = new Employee();
myEmployee.Name = "Jhon";
myEmployee.Insurance.PolicyId = 123 ;
myEmployee.Insurance.PolicyName = "Life Time" ;

C# 正在抱怨,我知道如何通过创建 Insurance 类的实例来解决它。

我的问题是我能否以我想在主程序中使用 like 的方式分配字段的值

**

myEmployee.Insurance.PolicyId = 123 ;
myEmployee.Insurance.PolicyName = "Life Time" ;

** 我试过

 public class Employee
    {

        public Employee()
        {
            Insurance Insurance = new Insurance();
        }

        public String Name;
        public Insurance Insurance;



        public class Insurance
        {
            public int PolicyId;
            public String PolicyName;
        } 
    }

在我尝试的主要方法中

class Program
    {
        static void Main(string[] args)
        {
            var joe = new Employee();
            joe.Name = "Joe";
            joe.Insurance.

        }

我得到这个错误-

错误 2“ConsoleApplication1.Employee.Insurance”和“ConsoleApplication1.Employee.Insurance”之间的歧义 c:\users\lenovo\documents\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 15 17 ConsoleApplication1

4

4 回答 4

16

您可以在 Employee 的构造函数中实例化 Insurance,因此它会自动为您完成。您可以为其提供默认值,以确保在以后访问时尚未将其定义为有效。

public class Employee
{
    Insurance Insurance { get; set; }

    public Employee()
    {
        this.Insurance = new Insurance() { PolicyId = -1 };
    }
}

public class Insurance
{
    public int PolicyId { get; set; }
    public string PolicyName { get; set; }
}

或者保持类嵌套:

public class Employee
{
    Insurance InsurancePolicy { get; set; }

    public Employee()
    {
        this.InsurancePolicy = new Insurance() { PolicyId = -1 };
    }
    public class Insurance
    {
        public int PolicyId { get; set; }
        public string PolicyName { get; set; }
    }
}
于 2013-08-08T18:32:27.000 回答
12

无需更改您的Employee类,您可以使用对象初始化器:

var myEmployee = new Employee 
{
    Name = "Jhon",
    Insurance = new Insurance
    {
        PolicyId = 123,
        PolicyName = "Life Time"
    }
};

或者,也许最好是,您可以让Employee该类在其构造函数中创建一个新实例Insurance(如在其他答案中),或者另一种选择是在Insurance属性 getter 本身中执行它,因此只有在您使用时才会实例化它它。这是后者的一个例子:

class Employee 
{
    private Insurance insurance;

    public Insurance Insurance
    {
        get
        {
            if (insurance == null)
            {
                insurance = new Insurance();
            }
            return insurance;
        }
    }
}

最后,我建议您不要构建具有所有公共字段的类,除非您真的知道这是您想要的。相反,我会考虑在字段上使用属性。我已将其他人的建议纳入以下代码,并提供了我自己的:

public class Employee
{
    public Employee() 
    {
        this.Insurance = new Insurance();
    }

    // Perhaps another constructor for the name?
    public Employee(string name)
        : this()
    {
        this.Name = name;
    }

    public string Name { get; set; }
    public Insurance Insurance { get; private set; }
}

public class Insurance
{
    public int PolicyId { get; set; }
    public string PolicyName { get; set; }
}
于 2013-08-08T18:30:52.287 回答
6

当然,但是你将如何分配属于一个null对象的东西呢?您需要InsuranceEmployee的构造函数中实例化。

public Employee()
{
     this.Insurance = new Insurance();
}

编辑关于您的评论:按照这种方法,您能够myEmplyee.Insurance.PolicyID使用两个点进行访问。构造函数Employee 的类中,所以一旦你实现它,你就不需要输入任何你已经尝试过的东西。

于 2013-08-08T18:31:07.697 回答
1

您可以为您的员工编写一个将实例化的构造函数Insurance

public class Employee
{
    public Employee()
    {
        this.Insurance = new Insurance();
    }
    public string Name;
    public Insurance Insurance;
}
于 2013-08-08T18:31:49.050 回答