如何限制用户为类创建对象但允许访问该类中的方法 [英] how to restrict the user to create an object for class but allow to access methods in that class

查看:73
本文介绍了如何限制用户为类创建对象但允许访问该类中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目提供

class A

{

a () {-----}

}

提供项目是 CLASS LIBRARY PROJECT





项目使用



班级使用

{

// 我必须在这里使用

} < br $> b $ b

USAGE 项目是 CONSOLE PROJECT



UseA 类中,我应该能够调用方法 a() 而无需创建对象 A类

Project PROVIDE
class A
{
a(){-----}
}
PROVIDE Project is a CLASS LIBRARY PROJECT


Project USAGE

Class UseA
{
//I have to use here
}

USAGE Project is a CONSOLE PROJECT

In UseA class I should be able to call method a() without creating an object to class A

推荐答案

这个问题没有明确的意义,因为你可能通过这种限制来表示不同的事情。



首先,该类可以声明为static。但它并不完全意味着实例的创建是受限制的,这意味着该类不能拥有实例。更重要的是,这样的类只能有实例成员,这意味着类本身的实例本身就没有意义,即使是允许的也是如此。这是你的问题和最简单的解决方案中最微不足道的意思。



如果你创建一个非静态类,我们将讨论一个非常不同的主题。首先,如果类是实例类,那么使用没有任何实例的类是很有可能的,但是具有这样的类将没有实际意义。如果你从不使用任何实例,为什么要有任何实例(非静态)成员?无论如何都不能使用这些成员。如果你不添加任何实例方法,这样的类在逻辑上等同于一个静态类,所以只有当你声明它是静态的时候它才会有意义,所以我们来讨论前面讨论过的静态类。



因此,我们将出现限制创建实例的唯一可能性:此类实例确实存在,但不能由用户直接创建。通常,只有在使用OOP时才有意义。一个这样的例子是派生自 System.Net.WebRequest 的类:

https://msdn.microsoft.com/en-us/library/system.net.webrequest%28v=vs.110 %29.aspx [ ^ ]。



你可以看到,这个类是抽象的,并且派生了非抽象类。没有类有构造函数(不计算过时的,让我们忘记它们)。实例是间接创建的:通过几个 System.Net.WebRequest 方法。事实上,返回对象的运行时类一如既往地是非抽象派生类,并且根据URI选择此类。用户不可能直接创建实例(并且由于提供了不匹配的类型和URI而不可能出错,这就是这个限制的全部目的)。



那么,限制本身是如何实现的?方法如下:你1)定义一个 private 无参数构造函数; 2)没有定义其他构造函数。第一项很重要,否则默认构造函数将自动创建并可访问;所以这是避免它的方法。



请注意,你所询问的限制本身与OOP,抽象和继承的非抽象类无关;这纯粹是正式的。但是你需要记住这种限制的目的。



-SA
This "problem" does not make definitive one sense, because you could mean different things by such "restriction".

First and foremost, the class can be declared static. But it does not exactly mean that creation of instances is "restricted", it means that the class cannot have instances. More importantly, such class can have only instance members, and that means that the instance of the class itself simply would make no sense, even it if was allowed. This is the most trivial meaning of your question and most trivial solution.

If you create a non-static class, we would discuss a very different topic. First of all, if the class is the instance class, using such class without any instances is quite possible, but having such class would make no practical sense. If you never use any instance, why having any instance (non-static) members? Such members could not be used anyway. And if you don't add any instance method, such class would be logically equivalent to a static class, so it would make some sense only if you declare it static, so we come the the previously discussed case of a static class.

Therefore, we are coming to the only possibility of "restricted" creation of instances: such instances really exist, but cannot be created by the user directly. Usually, it makes sense only when OOP is used. One such example is the classes derived from System.Net.WebRequest:
https://msdn.microsoft.com/en-us/library/system.net.webrequest%28v=vs.110%29.aspx[^].

Is you can see, this class is abstract and has derived non-abstract classes. None of the classes have constructors (not counting obsolete ones, let's just forget them). The instances are created indirectly: through several System.Net.WebRequest methods. In fact, the runtime class of the returned object is, as always, non-abstract derived class, and this class is chosen depending on the URI. The user has no possibility to create an instance directly (and no possibility to make a mistake due to unmatched type and URI supplied, this is the whole purpose of this "restriction").

So, how the restriction itself implemented? Here is how: you 1) define a private parameterless constructor; 2) define no other constructors. Firs item is important because otherwise the default constructor will be automatically created and accessible; so this is the way to avoid it.

Note that the "restriction" you asked about has itself nothing to do with OOP, abstract and inherited non-abstract classes; it is purely formal. But you need to keep in mind the purpose of such "restriction".

—SA


这篇关于如何限制用户为类创建对象但允许访问该类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆