繁体   English   中英

监控为普通班

[英]class Monitor as normal class

我正在阅读此链接: http : //eycenlearnstation.blogspot.nl/

他们在Visual Studio 2005博客上使用

在博客的某个地方,他们描述了这一行:

 Monitor myMonitor = new Monitor(ipAddr);

但是Monitor是静态类。 所以我的问题是:

在Visual Studio 2005中,Monitor类不是静态类吗?

还是在博客中只是一个错误?

谢谢

我不知道哪个Monitor类正在讨论整篇文章,但这不是Monitor线程方法。 System.Threading.Monitor始终没有公共构造函数。

在.NET 1.x中,它不是一个静态类,因为根本没有静态类 (它们是在.NET 2.0中引入的)。

您可以在旧的MSDN文档(C#2.0语言和编译器的新增功能)中看到这一点

静态类静态类是声明包含无法实例化的静态方法的类的一种安全便捷的方法。 在C#1.2版中,您应将类构造函数定义为私有,以防止实例化该类。

并且,@ Jcl添加了一些注释:

实际上,.NET 1.1的System.Threading.Monitor确实有一个私有构造函数(来自doc: 无法创建Monitor类的实例。

System.Threading.Monitor类在.NET Framework( .NET 2.0文档 )上一直是并且一直是静态的,但是,在链接的教程中,它清楚地显示了正在创建一个名为Monitor的不同类。

这是该页面的引文:

Monitor对象有什么用? 那是什么名字空间? 好吧,这就是我们接下来要创建的类。

在该文本下有一个实现:

class Monitor
{
  // Will store the IP address passed to it
  IPAddress ipAddress;

  // The constructor sets the IP address to the one retrieved by the instantiating object
  public Monitor(IPAddress address)
  {
      ipAddress = address;
  }
  /* ... etc. ... */

更新根据其他答案,似乎在.NET 1.1中不是静态的(因为.NET 1.1没有静态类,但是其所有方法也都是静态的)...那么,其余的答案是对,它只是不引用.NET Framework类

我在页面上的类定义中看到了这一点; 它不是静态的,因此可以使用new关键字实例化:

class Monitor
{
    // Will store the IP address passed to it
    IPAddress ipAddress;

    // The constructor sets the IP address to the one retrieved by the instantiating object
    public Monitor(IPAddress address)
    {
        ipAddress = address;
    }
...

The System.Threading.Monitor类在.NET 1.0中不是静态的,因为在该版本中没有静态类,因此在引入静态类时在.NET 2.0中将其标记为静态:

来源MSDN

这是为了回答您的问题。 作为该主题中使用的Monitor类,它是一个用户定义的类,名为Monitor

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM