【发布时间】:2010-08-29 03:24:27
【问题描述】:
我使用的是 C#,我没有太多经验(到目前为止,我主要使用 java/php/javascript)
我想要的是一个类,我在其中保存一些数据,这些数据只能被另一个类写入,但仍然可以被程序中的其他类读取。
类似这样的:
public class DataObtainer{
DataItem[] Items;
public DataObtainer(){
Items = new DataItem[20];
}
public void Update(){
Items[0].SomeProperty = 5;//Being able to change SomeProperty
}
//Class only contains properties
public class DataItem{
public int SomeProperty;
}
}
public class AnyOtherClass{
public void SomeMethod(){
DataObtainer do = new DataObtainer();
//What I want:
DataItem di = do.items[0];
Console.WriteLine(di.SomeProperty);//Being able to read SomeProperty
di.SomeProperty = 5;//Not allow this, not being able to change SomeProperty
}
}
【问题讨论】:
标签: c# access-modifiers