Java 基础知识 package 和 namespace

我是在学习Java 的时候,接触的这两个概念

package

我 们 把 相 关 的 或 者 属 于 一 类 的 c l a s s 放 到 一 个 p a c k a g e 中 \color{red}{我们把相关的或者属于一类的class 放到一个package 中 } classpackage

例如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} packagenamespace

来看一个例子:

使用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.

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值