我们的商业模式中有一个学生课程。令我感到奇怪的是,如果我们从另一个学生操纵一个学生,则学生的私人成员是可见的,这是为什么?
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);
}
}
我可以对此的设计考虑/影响有一些想法吗?看来你不能向你的兄弟姐妹隐瞒信息。有没有办法将字段或成员标记为对同一类的其他实例隐藏?
有一个简单的方法可以确保这一点:
不要搞乱同一类的其他实例的私有成员。
说真的——你是写这篇文章的人Student
code.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)