这里的 new() 函数是什么?

发布于 2024-10-05 15:47:24 字数 552 浏览 4 评论 0原文

我一直在学习设计模式,并且在类中看到了这样的方法调用:

class Client: SubjectAccessor {
    static void Main() {
        Console.WriteLine("Proxy Pattern\n");

        ISubject subject = new Proxy();
        Console.WriteLine(subject.Requesy());

        subject = new(); //Here is what I am asking
        COnsole.WriteLine(subject.Request());
    }
}

正如您所看到的,那里有一个 subject = new(); 调用,我想知道它是否正在创建一个新的Proxy 的实例或其他东西。我还没有找到任何与此相关的内容。

非常感谢您的帮助。

如果您需要,我可以粘贴整个代码,或者实际上它写在一本书上,所以我需要在这里写下来。

谢谢。

I've been learning design patterns and I saw such a method call from a class :

class Client: SubjectAccessor {
    static void Main() {
        Console.WriteLine("Proxy Pattern\n");

        ISubject subject = new Proxy();
        Console.WriteLine(subject.Requesy());

        subject = new(); //Here is what I am asking
        COnsole.WriteLine(subject.Request());
    }
}

As you can see there is a subject = new(); call there and I am wondering whether it is creating a new instance of Proxy or something else. I've not found anything related to this.

Your help is much appreciated.

If you need, I can paste the whole code or actually it is written on a book so I need to write it down here.

Thanks.

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

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

发布评论

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

评论(5

卷耳 2024-10-12 15:47:24

这是书中的一个错字。目前没有有效的 C# 版本(它应该引发“Type Expected”编译器错误)。如果没有上下文,就不可能知道它应该是什么。

It is a typo in the book. There is no current version of C# in which that is valid (it should raise a "Type expected" compiler error). Without context it is impossible to know what it should be.

幻想少年梦 2024-10-12 15:47:24

AFAIK 这是错误的,该代码甚至无法编译。

C# 中的 new 关键字只能具有此链接中描述的 3 个含义:
http://msdn.microsoft.com/en -us/library/51y09td4%28v=VS.80%29.aspx

AFAIK it's wrong, and that code won't even compile.

The new keyword in C# can have only the 3 meanings described in this link:
http://msdn.microsoft.com/en-us/library/51y09td4%28v=VS.80%29.aspx

清风夜微凉 2024-10-12 15:47:24

我是那本书的技术编辑;现在我面前就有一份副本。我的副本说:

class Client : SubjectAccessor { 
  static void Main() {
    Console.WriteLine("Proxy Pattern\n");

    ISubject subject = new Proxy();
    Console.WriteLine(subject.Request());
    Console.WriteLine(subject.Request());

    ProtectionProxy subject = new ProtectionProxy();
    Console.WriteLine(subject.Request());

现在,这里有一个错误;变量“subject”已声明两次。显然我在审阅这本书时没有发现这个错误。 (这里正确的做法是从“subject”的第二个声明中删除类型)。

但是,这不是您报告的错误。

确定这不是您的文案所说的吗?我有 2007 年 12 月的第一版;你有什么版本?也许有人试图在以后的版本中纠正这个错误并搞砸了?我的猜测是,有人试图通过从错误行中删除两个提到的 ProtectionProxy 类型来纠正错误,而不是删除第一个

I was the technical editor of that book; I have a copy right in front of me now. My copy says:

class Client : SubjectAccessor { 
  static void Main() {
    Console.WriteLine("Proxy Pattern\n");

    ISubject subject = new Proxy();
    Console.WriteLine(subject.Request());
    Console.WriteLine(subject.Request());

    ProtectionProxy subject = new ProtectionProxy();
    Console.WriteLine(subject.Request());

Now, there is an error here; the variable "subject" has been declared twice. Apparently I did not catch the error when I reviewed the book. (The correct thing to do here is to remove the type from the second declaration of "subject").

However, that is not the error that you are reporting.

Are you sure that is not what your copy says? I have the December 2007 first edition; what edition do you have? Perhaps someone attempted to correct this error in a later edition and messed it up? My guess would be that someone tried to correct the error by removing both mentions of the ProtectionProxy type from the erroneous line rather than removing the first one.

半衬遮猫 2024-10-12 15:47:24

这就是代理类。以前从未见过这样的语法。最好不要使用这样的东西,因为它只会降低可读性。

That would be the proxy class. never seen such syntax before tho. Best not to use such things as it will only reduce readability..

旧人九事 2024-10-12 15:47:24

我看不出它是如何编译的。在此上下文中,“new”是 new 运算符,并且始终需要符合 C# 语法的类型名称。

I can't see how that compiles. "new" in this context is the new operator and this always expects a typename per C# syntax.

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