使用带有Java导入语句的通配符并不错。

在Clean Code中,Robert C. Martin实际上建议使用它们以避免长时间导入列表。

这是建议:

J1: Avoid Long Import Lists by Using

Wildcards

If you use two or more classes from a

package, then import the whole package

with

import package.*;

Long lists of imports are daunting to

the reader. We don’t want to clutter

up the tops of our modules with 80

lines of imports. Rather we want the

imports to be a concise statement

about which packages we collaborate

with.

Specific imports are hard

dependencies, whereas wildcard imports

are not. If you specifically import a

class, then that class must exist. But

if you import a package with a

wildcard, no particular classes need

to exist. The import statement simply

adds the package to the search path

when hunting for names. So no true

dependency is created by such imports,

and they therefore serve to keep our

modules less coupled.

There are times when the long list of

specific imports can be useful. For

example, if you are dealing with

legacy code and you want to find out

what classes you need to build mocks

and stubs for, you can walk down the

list of specific imports to find out

the true qualified names of all those

classes and then put the appropriate

stubs in place. However, this use for

specific imports is very rare.

Furthermore, most modern IDEs will

allow you to convert the wildcarded

imports to a list of specific imports

with a single command. So even in the

legacy case it’s better to import

wildcards.

Wildcard imports can sometimes cause

name conflicts and ambiguities. Two

classes with the same name, but in

different packages, will need to be

specifically imported, or at least

specifically qualified when used. This

can be a nuisance but is rare enough

that using wildcard imports is still

generally better than specific

imports.

Java为啥不建议用通配符_为什么在Java导入语句中使用通配符不好?相关推荐

  1. MySql的like语句中的通配符:百分号、下划线和escape

    MySql的like语句中的通配符:百分号.下划线和escape %:表示任意个或多个字符.可匹配任意类型和长度的字符. Sql代码 select * from user where username ...

  2. java做日历怎么对齐日期_如何使用Java日历从日期中减去X天?

    如何使用Java日历从日期中减去X天? 有人知道使用Java日历从日期中减去X天的简单方法吗? 我无法找到任何允许我直接从Java中的日期减去X天的函数. 有人能指出我正确的方向吗? 9个解决方案 2 ...

  3. java守护线程与用户线程_详解Java线程-守护线程与用户线程

    干java 开发这么多年, 之前一直没留意java 进程还区分守护进程和用户进程.守护进程这个概念最早还是在linux系统中接触的,直到近期使用java开发心跳检测功能时,使用Timer时才发现原来j ...

  4. java 电池类类代码举例_教案:Java第7章 类的方法

    授课教师:牟勇 课时:100分钟 l 本章技能目标 n 会定义和使用类的方法 n 理解变量作用域 n 会创建包组织Java工程 n 会添加类方法的JavaDoc注释 n 会阅读JavaSE6文档 l ...

  5. Java每日一讲讲什么好_撩课-Java每天10道面试题第1天

    1.简述JDK.JRE.JVM? 一.JDK JDK(Java Development Kit) 是整个JAVA的核心, 包括了Java运行环境(Java Runtime Envirnment), 一 ...

  6. java 用程序代码解释继承_关于初级java程序员笔试题

    关于初级java程序员笔试题 Sun 认证Java程序员考试内容涉及Java所有相关知识.编程概念及applet开发技巧.下面是小编整理的关于初级java程序员笔试题,欢迎大家参考! 第一题:判断题 ...

  7. java程序的装载与检查_浅谈Java类型装载、连接与初始化

    类型装载.连接与初始化 Java虚拟机通过装载.连接和初始化一个Java类型,使该类型可以被正在运行的Java程序所使用.其中装载就是把二进制形式的Java class文件读入Java虚拟机中去;连接 ...

  8. java如何写线程外部类_廖雪峰Java读书笔记(六)--多线程(或称并发)

    1. 多线程基础 首先要明白一些概念: 进程:把一个任务称为一个进程,浏览器就是一个进程,视频播放器是另一个进程,类似的,音乐播放器和Word都是进程. 线程:某些进程内部还需要同时执行多个子任务.例 ...

  9. Java课程设计和sql数据库_数据库SQLserver+java课程设计

    [实例简介] SQL和java做的课程设计-学生信息管理系统.含源代码,含数据表. [实例截图] [核心代码] 数据库课程设计 └── 数据库课程设计 ├── DSA.class ├── DSA.ja ...

最新文章

  1. redis 集群搭建_一文轻松搞懂redis集群原理及搭建与使用
  2. 收藏:PCWorld:火狐浏览器已宣告死亡
  3. 图解python_可视化图解Python科学计算包NumPy
  4. 3PAR副总裁谈09年存储虚拟化
  5. SDL附着window窗口的方法
  6. Ubuntu20.04之安装VirtualBox虚拟机
  7. Silverlight 解谜游戏 之七 放大镜(3)
  8. Mac如何为应用单独设置语言?
  9. .net core不能使用httpget_.NET Core使用Nlog记录日志
  10. mysql查询所有男生中姓王的_数据库6.22
  11. 四轴飞行器——转动惯量
  12. ghost之后仍然中病毒----与病毒的斗争
  13. android webview加载图片不显示,解决android webview中图片不显示问题
  14. ★如何解释特修斯之船问题? /编
  15. Roy Li:币市里,我相信社区的力量和草根的崛起
  16. 计算机上DEL和INS怎么转换,HGVS命名之【缺失插入】Deletion-insertion (delins/indel)
  17. Linux 进程内存布局(一)
  18. HTML学习记录三 :创建电子邮件链接
  19. 如何将计算机网络设置为家庭网络连接打印机共享,怎样设置家庭网络打印机共享...
  20. html5手机号输入框,input输入框限制(座机,手机号码)

热门文章

  1. 一起学nRF51xx 1 - GPIO
  2. windows下Pycharm中安装Pygame
  3. 【arduino】继续用arduino玩CyberPi童芯派之DA音频信号播放
  4. 【arduino】arduino代码运行时间测试函数,代码性能运行时间测试方法
  5. 【测试】有道云笔记数据导出备份功能体验测试
  6. 数字通信介绍(4) OFDM为何如此热门?
  7. Java基于自定义注解的面向切面的实现
  8. VS代码提示不出现或者提示变成英文或者各种奇葩问题的解决
  9. URI是什么意思?URI和URL有什么区别?
  10. 2017-01-09