我们的商业模式中有一个学生课程。有件事让我觉得很奇怪,如果我们从另一个学生中操纵一个学生,学生的私人成员是可见的,这是为什么呢?
class Program {
static void Main(string[] args) {
Student s1 = new Student();
Student s2 = new Student();
s1.SeePrivatePropertiesAndFields(s2);
}
}
public class Student {
private String _studentsPrivateField;
public Student() {
_studentsPrivateField = DateTime.Now.Ticks.ToString();
}
public void SeePrivatePropertiesAndFields(Student anotherStudent) {
//this seems like these should be private, even from the same class as it is a different instantiation
Console.WriteLine(anotherStudent._studentsPrivateField);
}
}
我可以对此的设计考虑/影响有一些想法吗?看来您无法向您的兄弟姐妹隐瞒信息。有没有办法将字段或成员标记为对同一类的其他实例隐藏?