这是不好的编程?扫描仪作为全局变量 [英] is this bad programming ? scanner as global variable

查看:100
本文介绍了这是不好的编程?扫描仪作为全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将一个输入扫描器(例如键盘)声明为一个类的全局变量被认为是不好的编程习惯?例如:

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);

我使用各种方法输入大量数据,而且看起来好像更容易, to each method

Im working with alot of input from various methods, and just seems alot easier then having to send the keyboard to each method

推荐答案

使用全局变量似乎更容易,但从长远来看,它可以使代码很难维护,你有没有想过创建一个类来处理键盘输入?通过对问题的良好分离,您最终得到的代码更清晰。

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.

https://en.wikipedia.org/wiki/Separation_of_concerns

这篇关于这是不好的编程?扫描仪作为全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆