简体   繁体   English

监控为普通班

[英]class Monitor as normal class

I am reading this link: http://eycenlearnstation.blogspot.nl/ . 我正在阅读此链接: http : //eycenlearnstation.blogspot.nl/

They are using on the blog visual studio 2005 他们在Visual Studio 2005博客上使用

and somewhere in the blog they describe this line: 在博客的某个地方,他们描述了这一行:

 Monitor myMonitor = new Monitor(ipAddr);

But Monitor is a static class. 但是Monitor是静态类。 So my question is: 所以我的问题是:

Was in Visual studio 2005 the class Monitor not a static class? 在Visual Studio 2005中,Monitor类不是静态类吗?

Or is it just a mistake in the blog? 还是在博客中只是一个错误?

Thank you 谢谢

I don't know which Monitor class is talking about the whole article, but it's not the Monitor threading approach. 我不知道哪个Monitor类正在讨论整篇文章,但这不是Monitor线程方法。 System.Threading.Monitor has always had no public constructor. System.Threading.Monitor始终没有公共构造函数。

In .NET 1.x wasn't a static class because there were no static classes at all (they were introduced in .NET 2.0). 在.NET 1.x中,它不是一个静态类,因为根本没有静态类 (它们是在.NET 2.0中引入的)。

You can see that on this old MSDN document (What's New in the C# 2.0 Language and Compiler) : 您可以在旧的MSDN文档(C#2.0语言和编译器的新增功能)中看到这一点

Static Classes Static classes are a safe and convenient way of declaring a class containing static methods that cannot be instantiated. 静态类静态类是声明包含无法实例化的静态方法的类的一种安全便捷的方法。 In C# version 1.2 you would have defined the class constructor as private to prevent the class being instantiated. 在C#1.2版中,您应将类构造函数定义为私有,以防止实例化该类。

And, @Jcl has added in some comment: 并且,@ Jcl添加了一些注释:

In fact, .NET 1.1's System.Threading.Monitor did have a private constructor (from the doc: An instance of the Monitor class cannot be created. ) 实际上,.NET 1.1的System.Threading.Monitor确实有一个私有构造函数(来自doc: 无法创建Monitor类的实例。

The System.Threading.Monitor class is and has always been static on the .NET Framework ( documentation for .NET 2.0 ), however, in the linked tutorial, it clearly shows a different class named Monitor is being created. System.Threading.Monitor类在.NET Framework( .NET 2.0文档 )上一直是并且一直是静态的,但是,在链接的教程中,它清楚地显示了正在创建一个名为Monitor的不同类。

This is a quote from the page: 这是该页面的引文:

What's the deal with the Monitor object? Monitor对象有什么用? What namespace is that? 那是什么名字空间? Well that's the class we're going to create next. 好吧,这就是我们接下来要创建的类。

And there's an implementation below that text: 在该文本下有一个实现:

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. ... */

Update : as per other answer, it seems it was not static in .NET 1.1 (because .NET 1.1 didn't have static classes, however all of its methods were also static) ... then again, the rest of the answer is right, it's just not refering to the .NET Framework class 更新根据其他答案,似乎在.NET 1.1中不是静态的(因为.NET 1.1没有静态类,但是其所有方法也都是静态的)...那么,其余的答案是对,它只是不引用.NET Framework类

I'm seeing this for the class definition on the page; 我在页面上的类定义中看到了这一点; it isn't static, so it can be instantiated with the new keyword: 它不是静态的,因此可以使用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 class was not static in .NET 1.0 as there was not static classes at that version, it was marked as static in .NET 2.0 when static classes get introduced: The System.Threading.Monitor类在.NET 1.0中不是静态的,因为在该版本中没有静态类,因此在引入静态类时在.NET 2.0中将其标记为静态:

Source MSDN 来源MSDN

This is to answer you question. 这是为了回答您的问题。 As the Monitor class used in the topic, it is a user defined class named Monitor 作为该主题中使用的Monitor类,它是一个用户定义的类,名为Monitor

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

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