使方法/属性对一个类可见,对其他类隐藏

发布于 2024-07-13 12:04:56 字数 506 浏览 6 评论 0原文

我有一个类 Server ,它与 IRC 的服务器连接进行对话。 它保存已知用户的列表,并根据需要创建它们。

我有两个涉及 User 类的问题:

  1. 任何人都可以创建 User 的实例。 我只希望 Server 类能够执行此操作。
  2. 如果用户(User 描述的事物)更改了他/她的姓名(或其他信息,例如加入的频道),Server 类可以自行更改它。 然而,其他课程也可以! 我想禁止其他类接触此信息(使其只读)。

我该如何解决这两个问题? 在 C++ 中,可以通过使用 friend 关键字并将 ctor 和 setName (等)设为私有来解决。

C# 是否有一个关键字可以允许指定类访问某个方法? 这可以解决我的问题。

I have a class Server which talks to a server connection for IRC. It holds a list of known Users, and creates them as necessary.

I have two problems involving the User class:

  1. Anyone can create an instance of a User. I only want the Server class to be able to do this.
  2. If a user (the thing User describes) changes his/her name (or other info, like joined channels), the Server class can change it itself. However, other classes can, too! I want to disallow other classes from touching this information (making it read-only to them).

How can I solve these two problems? In C++, it can be solved by using the friend keyword and making the ctor and setName (and such) private.

Is there a C# keyword which can allow a certain method be accessable by a specified class? This would solve my problem.

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

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

发布评论

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

评论(3

孤蝉 2024-07-20 12:04:56

老实说,我发现源自 C++ 的 friend 访问是糟糕设计的症状。 你最好修复你的设计。

首先,谁真正关心是否有人创建了用户? 真的有关系吗? 我问这个问题是因为我们程序员有时会担心一些根本不会发生的事情,或者即使发生了也没关系。

如果您确实关心,请执行以下操作之一:

  • 将用户设置为界面。 服务器可以实例化一个实现它的私有类; 或者
  • 使 User 成为 Server 的内部类,没有公共构造函数,因此只有 Server 可以实例化它。

可见性黑客(C++ 中的朋友就是其中之一,Java 中的包访问都是很好的例子)只是自找麻烦,而不是一个好主意。

Honestly I find the friend access that originated from C++ to be symptomatic of bad design. You're better off fixing your design.

For a start, who really cares if someone creates a User? Does it really matter? I ask this because it seems that sometimes we programmers get caught up worrying about things that simply won't happen or, if they do, it doesn't matter.

If you really do care then do one of the following:

  • Make User an interface. Server can instantiate a private class that implements it; or
  • Make User an inner class of Server with no public constructors so only Server can instantiate it.

Visibility hacks (of which friends in C++ are one and package access in Java are both good examples) are simply asking for trouble and not a good idea.

冬天旳寂寞 2024-07-20 12:04:56

在 .NET 世界中,最接近friend的是internal可见性。

请注意,如果您的两个类位于不同的程序集中,则可以使用 InternalsVisibleTo 属性允许一个程序集对另一个程序集的内部可见。

The closest in the .NET world to friend is the internal visibility.

Note that if your two classes are in separate assemblies, you can use the InternalsVisibleTo attribute to allow one assembly visibility of the internals of the other.

自找没趣 2024-07-20 12:04:56

重新设计类层次结构可能是解决此问题的一个合理方法。 因为在我看来,您真正需要的是一个只读的 User 类(当它存在于 Server 类之外时)。

我可能会这样做:

// An abstract base class. This is what I'd use outside the 
// Server class. It's abstract, so it can't be instantiated
// on its own, and it only has getters for the properties. 
public abstract class User
{
   protected User()
   {
   }

   public string Name { get;}
   // Other get-only properties
}

public class ServerUser : User
{
   public ServerUser()
   {
   }

   public string Name { get; set;}
   // Other properties. 
}

然后让 Server 类创建 ServerUser 类,使用 Server 类来更改 ServerUser 类上的属性(例如更改用户名),但仅将 User 类暴露给外界。

Redesigning your class hierarchy is probably a fair solution to this problem. Because it sounds to me like what you really need is a read-only User class when it exists outside the Server class.

I would probably do something like this:

// An abstract base class. This is what I'd use outside the 
// Server class. It's abstract, so it can't be instantiated
// on its own, and it only has getters for the properties. 
public abstract class User
{
   protected User()
   {
   }

   public string Name { get;}
   // Other get-only properties
}

public class ServerUser : User
{
   public ServerUser()
   {
   }

   public string Name { get; set;}
   // Other properties. 
}

Then have the Server class created ServerUser classes, user the Server class to change properties on the ServerUser classes (like changing username) but only expose User classes to the outside world.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文