本文共 1591 字,大约阅读时间需要 5 分钟。
我是在学习Java 的时候,接触的这两个概念
package
我 们 把 相 关 的 或 者 属 于 一 类 的 c l a s s 放 到 一 个 p a c k a g e 中 \color{red}{我们把相关的或者属于一类的class 放到一个package 中 } 我们把相关的或者属于一类的class放到一个package中
例如Java 的 java.lang
Oracle 的描述是这样的
Provides classes that are fundamental to the design of the Java programming language.
The most important classes are Object which is the root of the class hierarchy, and Class, instances of which represent classes at run time. Frequently it is necessary to represent a value of primitive type as if it were an object. The wrapper classes Boolean, Character, Integer, Long, Float, and Double serve this purpose.
这是对Java language 极其重要的一个包
包含了Object 和基本数据类型int、double 等等
再来说namespace
在 不 同 的 p a c k a g e 中 , 则 是 不 同 的 n a m e s p a c e \color{red}{在不同的package 中,则是不同的namespace} 在不同的package中,则是不同的namespace
来看一个例子:
使用List 时,会要求你导入包
Eclipse 的提示项中有:
import ‘List’ (java.util)
import ‘List’ (java.awt)
相信大家都遇到过这样的情况
在不同的包中存在名字相同的List 类
那么区分它们的一个手段就是namespace(命名空间)
相 同 名 字 的 类 , 利 用 不 同 的 命 名 空 间 区 分 开 来 \color{blue}{相同名字的类,利用不同的命名空间区分开来} 相同名字的类,利用不同的命名空间区分开来
下面再给大家提供一些维基百科上的介绍
Namespaces are commonly structured as hierarchies to allow reuse of names in different contexts
a given set of objects have unique names so that they can be easily identified.
In computer programming, namespaces are typically employed for the purpose of grouping symbols and identifiers around a particular functionality and to avoid name collisions between multiple identifiers that share the same name.
In networking, the Domain Name System organizes websites (and other resources) into hierarchical namespaces.
转载地址:http://qoiyb.baihongyu.com/