日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2025/4/5 java 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java为啥不建议用通配符_为什么在Java导入语句中使用通配符不好? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用帶有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导入语句中使用通配符不好?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。