Java导入中的通配符如何工作? [英] How do wildcards in Java import work?

查看:37
本文介绍了Java导入中的通配符如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直注意到我的 import s出现了一些意外的结果,我希望了解发生了什么.我从以下内容开始:

I've been noticing some unexpected results with my imports, and I'm hoping to understand what's really going on. I started with the following:

import java.util.*;
import java.io.*;
import java.nio.*;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption.*;
import java.nio.file.Paths;

,然后发现 ArrayList ListIterator 不起作用...所以我添加了

and then found that ArrayList and ListIterator didn't work...so I added

import java.util.ArrayList;
import java.util.ListIterator;

一切正常.

我本以为 java.util.* 也将导入 ArrayList ListIterator .为什么不呢?

I would have assumed that java.util.* would have also imported ArrayList and ListIterator. Why didn't it?

我倾向于过于冗长地使用类/方法/变量名,并且在程序的其余部分中都找不到与保留字 close 相似的东西.

I've a tendency to be overly verbose with my class/method/variable names, and I didn't find anything in the rest of the program that was even close to a reserved word.

我在文档中找不到任何能说明为什么会发生这种情况的信息,并且关于Stack的大多数讨论都涉及使用*与显式编码的导入进行优化.

I can't find anything in the documentation that suggests why this would occur, and most of the discussion on Stack is about optimaztion using * vs. explicitly coded imports.

任何人都有任何想法,我还可以看一下以了解这种行为?

Anyone have any ideas what else I can look at to get an understanding of this behavior?

推荐答案

导入java.util.* 绝对导入 java.util.ArrayList 以及该软件包中的其他所有内容.请注意,没有子包的概念,因此它不会从 java.util.x java.util.y 导入任何内容,但是不会出现就是您的问题.

import java.util.* definitely imports java.util.ArrayList and everything else in that package too. Note that there's no concept of sub-packages, so it wouldn't import anything from java.util.x or java.util.y, but that doesn't appear to be the case with your issue.

如果代码无法正常工作,您还必须在代码上遇到其他问题,导入语句肯定会按照您的描述行事.

You must have some other problem wrong with your code if it's not working, the import statements definitely behave as you describe.

这篇关于Java导入中的通配符如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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