C#学习小记3 验证main()内可访问本类的私有属性
//实验1目的是验证main()内可访问本类的私有属性
using System;
public class Student
{
private int i;
public int I {
set{i=value;}
}
static void Main()
{ Student o=new Student();
o.i=9;
//Console.WriteLine(o.I);
Console.WriteLine(o.i);
}
}
//实验成功!可以!
//实验后感,很奇怪我声明的i是private 的,可是在实例o后居然可以使用.i,我想可能是因为在因为main()是类内的.
//对比实验2
using System;
public class Student
{
private int i;
public int I {
set{i=value;}
}
}
public class D
{static void Main()
{ Student o=new Student();
o.i=9;
//Console.WriteLine(o.I);
//Console.WriteLine(o.i);
}
}
//把main() 放到别的类内,这里的o.i 就抱错了.呵呵因为是在类外访问私有的属性!!