简体   繁体   English

如何从类的实例访问嵌套类?

[英]How to access nested class from instance of a class?

partial class Employee
{
    protected string empName;

    protected int empID = new int();

    protected float currPay;

    protected static int empAge;

    protected string empSNN;

   // these are nested classes

    public class BenefitPackage
    {
        // I want to access this class

        public enum BenefitPackageLevel
        {
            standard,Gold,Platinum
        }
        public double ComputePayDeduction()
        {
            return 125.0;
        }
    }

I'm trying to access the BenefitPackageLevel class through an instance of employees class like: 我正在尝试通过employees类的实例访问BenefitPackageLevel类,如:

Employee emp= new Employee() 
var benefitpackage= emp.BenefitPackage.BenefitPackageLevel.standard;

but why while I didn't define BenefitPackage as a static member, I just can access it through the class level like: 但是为什么虽然我没有将BenefitPackage定义为静态成员,但我可以通过类级别访问它,如:

Employee.BenefitPackage.BenefitPackageLevel.standard

Is it possible that nested classes are static by default? 嵌套类默认是静态的吗?

You are not accessing it as an static member. 您不是以静态成员身份访问它。 You are accessing standard through its outer types. 您通过其外部类型访问standard When you declare a nested type its scope is limited to the outer type so you must access it through its outer type. 声明嵌套类型时,其范围仅限于外部类型,因此您必须通过其外部类型访问它。

For example if you want to create an instance of BenefitPackage you should do it like this: 例如,如果要创建BenefitPackage的实例,您应该这样做:

var benefitPackage = new Employee.BenefitPackage();

So when you want to access standard as a value of BenefitPackageLevel enumeration you must use it like this: 因此,当您想要将standard作为BenefitPackageLevel枚举的值访问时,您必须使用它,如下所示:

var temp = Employee.BenefitPackage.BenefitPackageLevel.standard;

Nested types are inaccessible to external types unless you made them public. 除非您公开,否则外部类型无法访问嵌套类型。

Keep in mind that when you create an instance of an outer type it does not create an instance of its inner types. 请记住,在创建外部类型的实例时,它不会创建其内部类型的实例。

You have a couple of issues with your code. 你的代码有几个问题。 First you need to do this change to your BenefitPackage class: 首先,您需要对BenefitPackage类进行此更改:

public class BenefitPackage
{
   // Your code...

   public BenefitPackageLevel Level { get; set; }
}

Then you need to do make this change to your Employee class and add the following property: 然后,您需要对Employee类进行此更改并添加以下属性:

partial class Employee
{
   // Your code...

   public Employee()
   {
      this.EmoloyeeBenefitPackage = new BenefitPackage();
   }
   public BenefitPackage EmployeeBenefitPackage { get; set; }
}

Now you can do this: 现在你可以这样做:

var employee = new Employee();
employee.EmoloyeeBenefitPackage.Level = BenefitPackageLevel.Gold;
var level = employee.EmployeeBenefitPackage.Level;

I think you are confused. 我觉得你很困惑。 Because a type is defined within another type, it doesn't mean the outside type contains any instance information unless a field is explicitly defined. 因为类型是在另一种类型中定义的,所以它并不意味着外部类型包含任何实例信息,除非明确定义了字段。

For Example: 例如:

public class OutsideClass
{
    // Define field or properties to store nested types
    public Select Pick { get; set; }   
    public NestedClass Link { get; set; }
    // Type definitions
    public class NestedClass
    {
    }
    public enum Select
    {
        None,
        One,
        Many,
        All
    }
}

class Program
{
    static void Main(string[] args)
    {
        OutsideClass a = new OutsideClass();
        a.Pick=OutsideClass.Select.Many;
        a.Link=new OutsideClass.NestedClass();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM