public class Outer
{
public class Inner
{
public static string OtherValue { get { return SomeValue; } }
}
public static string SomeValue { get { return "Outer"; } }
}
Run Code Online (Sandbox Code Playgroud)
为什么以上编译?SomeValue是否超出内部范围且需要符合资格Outer.SomeValue?上面的内容与下面的内容基本相同(不会编译)?
public class Outer
{
public class Inner
{
public static string OtherValue { get { return Outer.SomeValue; } }
}
public static string SomeValue { get { return "Outer"; } }
}
Run Code Online (Sandbox Code Playgroud)
static这里有什么魔力?
它像是:
public class Super
{
public class Sub
{
public static string OtherValue { get { return Super.SomeValue; } }
}
public static string SomeValue { get { return "Outer"; } }
}
Run Code Online (Sandbox Code Playgroud)
随着Super控股静SomeValue
public class Super
{
public static string SomeValue { get { return "Outer"; } }
}
public class Sub
{
public static string OtherValue { get { return Super.SomeValue; } }
}
Run Code Online (Sandbox Code Playgroud)
现在用Sub和Super为public他们的静态属性是所有可见他们共同的范围之内.正如@jonskeet所指出的那样,编译器将首先搜索最适合SomeValue查找类的内容,Sub因为该值在该类中使用.
现在这样的东西不会编译:
public class Super
{
private static string SomeValue { get { return "Outer"; } }
}
public class Sub
{
public static string OtherValue { get { return SomeValue; } }
}
Run Code Online (Sandbox Code Playgroud)
然而,
public class Super
{
private static string SomeValue { get { return "Outer"; } }
public class Sub
{
public static string OtherValue { get { return SomeValue; } }
}
}
Run Code Online (Sandbox Code Playgroud)
由于课程的嵌套,上述情况很好.
| 归档时间: |
|
| 查看次数: |
404 次 |
| 最近记录: |