为什么 Java 没有“深"?通配符导入? [英] why doesn't Java have "deep" wildcard import?

查看:22
本文介绍了为什么 Java 没有“深"?通配符导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么import one.two.* 不包括import one.two.three.MyClass?Java 不应该有类似 import one.two.** 的东西吗?或者有什么理由(除了他们没有做,不是他们做不到)?

Why doesn't import one.two.* include import one.two.three.MyClass? Shouldn't Java have something like import one.two.**? Or is there any reason (other than they just didn't do, not that they couldn't do it)?

谢谢

推荐答案

Java 并不将包视为真正的相互子类化;虽然 java.utiljava.util.concurrency 可能看起来像第二个在某种程度上是第一个的一部分,但它们被视为完全独立的,并且点主要是为了整洁.

Java does not treat packages as truly subclassing each other; while java.util and java.util.concurrency might look like the second is somehow part of the first, they are treated as entirely independent and the dot is mostly there for neatness.

这意味着您不必害怕将您的类或接口命名为与稍后在某个超级包或子包中声明的另一个相同的类或接口,这也意味着您真的应该多写几个进口线.

This means you don't need to be afraid of naming your class or interface the same as another one declared in some super- or sub-package on a later date, and it also means you should really just write a couple extra lines of imports.

这个决定背后的原因,正如 Peter Lawrey 所解释的,源于 Java 对简单性的普遍倾向.最佳做法通常是无论如何都不要使用导入通配符.

The reasons behind this decision, as Peter Lawrey explained, stem from Java's general lean towards simplicity. Best practice is often to never use import wildcards at all anyway.

这篇关于为什么 Java 没有“深"?通配符导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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