类只能由“父级”写入类,但可以被其他类读取

发布于 2024-09-16 02:32:18 字数 729 浏览 3 评论 0原文

我正在使用 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
 }
}

I'm using C#, with which I don't have a lot of experience (I've mostly worked with java/php/javascript so far)

What I want is a class in which I save some data, this data can only be written by one other class, but still be read by other classes in the program.

Something like this:

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
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苄①跕圉湢 2024-09-23 02:32:18

使用接口。

public interface IData
{
   string Data1 { get;}
   int MoreData { get;}
}

class Data : IData
{
   public string Data1 { get; set;}
   public int MoreData {get; set;}
}

public class DataObtainer
{
   private Data[] items;
   public DataObtainer()
   {
      items = new Data[20];
   }
   public IEnumerable<IData> Items
   {
      get
      {
         return items;
      }
   }

   public void Update()
   {
      Items[0].MoreData = 5;//Being able to change MoreData
   }
}

public class AnyOtherClass
{
   public void SomeMethod()
   {
       DataObtainer do = new DataObtainer();
       //What I want:
       IData di  = do.Items.First();
       Console.WriteLine(di.MoreData);//Being able to read SomeProperty
       di.SomeProperty = 5;//this won't compile
   }
}

说明:

  1. 创建一个要提供给代码的接口 (IData)
  2. 在该接口的实现中创建 将实现
  3. 存储在获取器中
  4. 只允许其他代码访问该接口。这并不能让他们访问更改值。
  5. 如果需要的话,调用代码可以强制转换为实现,但是这样他们就违反了合同,所有的赌注都失败了。

Use an interface.

public interface IData
{
   string Data1 { get;}
   int MoreData { get;}
}

class Data : IData
{
   public string Data1 { get; set;}
   public int MoreData {get; set;}
}

public class DataObtainer
{
   private Data[] items;
   public DataObtainer()
   {
      items = new Data[20];
   }
   public IEnumerable<IData> Items
   {
      get
      {
         return items;
      }
   }

   public void Update()
   {
      Items[0].MoreData = 5;//Being able to change MoreData
   }
}

public class AnyOtherClass
{
   public void SomeMethod()
   {
       DataObtainer do = new DataObtainer();
       //What I want:
       IData di  = do.Items.First();
       Console.WriteLine(di.MoreData);//Being able to read SomeProperty
       di.SomeProperty = 5;//this won't compile
   }
}

Explaination:

  1. Create an interface which you are going to provide to code (IData)
  2. Create in implementation of that interface
  3. Store in the Obtainer the implementation
  4. Give other code access only to the interface. This doesn't give them access to change values.
  5. The calling code can cast to the implementation if it wants, but then they are breaking the contract and all bets are off.
江湖正好 2024-09-23 02:32:18

您应该将 DataItem 设为外部(非嵌套)抽象 类,然后创建一个继承它并提供公共变异器方法的内部(私有)类。

DataObtainer 中,您可以将对象强制转换为私有继承类并修改它们。

You should make DataItem an outer (non-nested) abstract class, then make an inner (private) class that inherits it and provides public mutator methods.

In DataObtainer, you can then cast the objects to the private inherited class and modify them.

等待圉鍢 2024-09-23 02:32:18

这种设计对我来说似乎很尴尬。您可以控制代码,因此除非您正在设计某种框架/API,否则您要求做的事情并不是真正必要的。如果类不应该能够修改属性,则不要修改该属性或不提供 setter。

也许您可以多解释一下您需要完成此任务的内容或原因,以帮助我们了解为您提供实现目标的最佳方法。

使用基本继承的简单示例

// Class used to read and write your data
public class DataBuilder : Data {
    public void SetValue(int value) {
        base.m_SomeValue = value; // Has access to protected member
    }
}

// Class used to store your data (READONLY)
public class Data {
    protected int m_SomeValue; // Is accessible to deriving class
    public int SomeValue {     // READONLY property to other classes
                               // EXCEPT deriving classes
        get {
            return m_SomeValue;
        }
    }
}

public class AnyOtherClass {
    public void Foo() {
        DataBuilder reader = new DataBuilder();
        Console.WriteLine(reader.SomeValue); // *CAN* read the value
        reader.SomeValue = 100; // CANNOT *write* the value
    }
}

That sort of design seems awkward to me. You are in control of the code, so what you are asking to do is not really necessary unless you are designing some sort of framework/api. If a class shouldn't be able to modify a property, don't modify the property or don't provide a setter.

Maybe if you can explain a bit more of what or why you need to accomplish this to help us understand the best approach to provide you for your goal here.

Simple Example using basic inheritance

// Class used to read and write your data
public class DataBuilder : Data {
    public void SetValue(int value) {
        base.m_SomeValue = value; // Has access to protected member
    }
}

// Class used to store your data (READONLY)
public class Data {
    protected int m_SomeValue; // Is accessible to deriving class
    public int SomeValue {     // READONLY property to other classes
                               // EXCEPT deriving classes
        get {
            return m_SomeValue;
        }
    }
}

public class AnyOtherClass {
    public void Foo() {
        DataBuilder reader = new DataBuilder();
        Console.WriteLine(reader.SomeValue); // *CAN* read the value
        reader.SomeValue = 100; // CANNOT *write* the value
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文