Java 这是糟糕的编程吗?扫描仪作为全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21198652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is this bad programming ? scanner as global variable
提问by Ris
Is it considered bad programming practice to have an input scanner (such as a keyboard ) declared as a global var for a class? such like:
将输入扫描器(例如键盘)声明为类的全局变量是否被认为是糟糕的编程实践?比如:
private static Scanner input = new Scanner(System.in);
Im working with alot of input from various methods, and just seems alot easier then having to send the keyboard to each method
我处理了来自各种方法的大量输入,看起来比将键盘发送到每种方法要容易得多
采纳答案by LuRsT
It seems a lot easier to use a global variable, but in the long run, it can make the code very hard to maintain, have you thought about creating a class to handle the keyboard input? By having a good separation of concerns, you end up with cleaner code.
使用全局变量看起来容易很多,但从长远来看,它会让代码很难维护,你有没有想过创建一个类来处理键盘输入?通过良好的关注点分离,您最终会得到更清晰的代码。
回答by Jaso333
Depending on how the object should be used would define where to put it.
根据对象应该如何使用将定义放置它的位置。
If the Scanner is something that there MUST be only one instance of, then consider making it a singleton instead of creating it using the constructor. The following link describes singletons:
如果 Scanner 必须只有一个实例,则考虑将其设为单例,而不是使用构造函数创建它。以下链接描述了单身人士:
http://www.javaworld.com/article/2073352/core-java/simply-singleton.html
http://www.javaworld.com/article/2073352/core-java/simply-singleton.html
Then, rather than having it as a static global, the Scanner class can have a public static method called 'getInstance'. Therefore, you aren't tieing the instance of a scanner to any particular location and whenever you need to use it, call Scanner.getInstance from anywhere to access the underlying instance of the class.
然后,Scanner 类可以有一个名为“getInstance”的公共静态方法,而不是将其作为静态全局变量。因此,您不会将扫描仪的实例绑定到任何特定位置,并且无论何时需要使用它,都可以从任何地方调用 Scanner.getInstance 以访问该类的底层实例。
回答by y?s??la
Overall it's ok since it's a very commonly used object in your application. However there are 2 problems you can face as far as I can see:
总体来说没问题,因为它是您的应用程序中非常常用的对象。但是,据我所知,您可能会遇到两个问题:
- Concurrent access and mutable state of Scanner can be a problem. You might want to synchronize that.
- Unit testing might be a problem since you can't override static members. It might still be ok if in classes that use it it can be overridden.
- Scanner 的并发访问和可变状态可能是一个问题。您可能想要同步它。
- 单元测试可能是一个问题,因为您不能覆盖静态成员。如果在使用它的类中可以覆盖它可能仍然没问题。
So it depends on the size of your app and how it's used in terms of multithreading. I would do it at home but not at work.
因此,这取决于您的应用程序的大小以及它在多线程方面的使用方式。我会在家里做,但不会在工作。
回答by Exorcismus
It's best if you created a special class for getting Inputs and/or produce Outputs for example
例如,最好创建一个特殊的类来获取输入和/或生成输出
class IO{
Scanner scanner ;
public Scanner getScanner(){
return new Scanner();
}
public Scanner getScanner(File file){
return new Scanner(new FileReader(file));
}
... // other types of scanners
}