将 Scanner 对象作为构造函数参数传递给 UserInterface 类。

huangapple go评论75阅读模式
英文:

Passing Scanner object as a constructor parameter to the UserInterface class

问题

当我正在观看关于如何将用户界面与程序逻辑分离的YouTube视频时,视频中的那位讲解者将扫描器(Scanner)类对象作为参数传递给了主方法中的UserInterface类的构造函数。就像这样:

public class UserInterface {
    TodoList list;
    Scanner input;

    UserInterface(TodoList list, Scanner input){
        this.list = list;
        this.input = input;
    }
}

我的问题是,为什么他没有在UserInterface类中创建一个Scanner类对象,而是从主类中将Scanner对象作为参数传递呢?

英文:

While I was watching a youtube video on how to seperate UserInterface from the program logic, the guy on the video passed the scanner class object as a parameter to the UserInterface class's constructor from the main method. Like this:

public class UserInterface {
TodoList list;
Scanner input;

UserInterface(TodoList list,Scanner input){
    this.list=list;
    
    this.input=input;
    
}
}

My question is that why didn't he create a Scanner class object in the UserInterface class instead of passing the Scanner object as a parameter from Main class.

答案1

得分: 1

因为 Scanner 是一个资源。这是一个非常奇怪的资源;通常资源是短暂的资源(也就是说,在应用程序完全退出之前,不再需要该资源),并且是整个类中的一种(也就是说,你可以打开 1 个文件,也可以是一百万个文件。概念“文件”是一个多重性的集合;你可以拥有很多文件)。System.in 很奇怪;你不希望关闭它,并且永远只有一个。

这使它成为了一个奇怪的资源。

然而,它就是一个资源,最好将资源视为单个实体。你不会希望同时创建 2 个单独的文件阅读器。出于同样的原因,你也不会想要 2 个扫描器。扫描器可以(而且确实会!)进行缓存,因此如果你在一个扫描器上调用 .hasNextInt(),然后在另一个扫描器上调用 .next(),事情会变得奇怪,因为扫描器并不是为此而设计的。

让我试着这样解释:System.in 是一个全局常量。因此,消耗 System.in 的任何扫描器也应该是全局的,因为它被指定为可能会缓存内容的东西。

另外,这也为你的 UserInterface 类提供了在不同输入上操作的机会。也许你希望将来从互联网连接获取输入,或者从批处理文件中获取一系列命令。或者,更简单地说,你想编写一个自动化测试。

Scanner 并不固有意味着:“来自标准输入”。你可以使用许多东西来创建 Scanner:文件、网络连接、硬编码字符串,任何你想要的东西。

英文:

Because Scanner is a resource. It's a really weird resource; normally resources are for a fleeting resource (as in, the idea is to stop needing that resource well before the application exits entirely), and one amongst an entire class (as in, you can open 1 file, or a million. The concept 'file' is a multitude; you can have many files). System.in is weird; You don't want to close it, and there is only ever one.

That makes it a weird resource.

Nevertheless, it is one, and resources are best treated as singular entities. You wouldn't want to create 2 separate file readers simultaneously. For the same reason you don't want 2 scanners. Scanners can (and do!) cache, so if you call, say, .hasNextInt() on one scanner and then call .next() in the other, things become weird, because scanner wasn't designed to do that.

Let me try to put it this way: System.in is a global constant. Therefore any scanner that consumes System.in should also be, because it is specified as a thing that may cache things.

Separately, this also opens the door to allowing your UserInterface class to operate on different input. Maybe you want to, someday, take input from an internet connection, or a batch file with a series of commands in it. Or, even simpler, you want to write an automated test.

Scanner doesn't inherently mean: "From standard input". You can make a Scanner out of many things: Files, network connections, hardcoded strings, anything you want, really.

huangapple
  • 本文由 发表于 2020年10月17日 20:16:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64402336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定