如何使用Java包? [英] How to use Java packages?

查看:130
本文介绍了如何使用Java包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们在Java中使用包?
如何使用它们?

Why do we use packages in Java? How to use them?

推荐答案

软件包主要是只是组织代码的一种方式。 JDK有数千个类,大型应用程序有数千个类。为什么想要将这些组织成某种层次结构,以便您轻松找到您感兴趣的课程?

Packages are mostly just a way of organizing code. The JDK has thousands of classes in, and a large application has thousands more. Why would you not want to organize those into some sort of hierarchy allowing you to find the classes you're interested in easily?

参与Java中的访问控制(但不是在.NET中,有趣的是) - 但我要说的主要目的是帮助有意义地组织他们的代码。

Packages also participate in access control in Java (but not in .NET, interestingly) - but I'd say the main purpose is to help humans organize their code meaningfully.

意味着您可能偶尔需要或需要多个包中的相同类名 - 其中包名有效地提供了上下文。现在,如果你在单个代码库中为单个应用程序工作,那么通常要试图避免 - 但如果你有一个大型代码库,许多不同的应用程序使用它的许多不同部分,这可能比尝试为每个类都有一个唯一的名称更好。 (一个典型的例子是用户界面代码 - 只是查看.NET库,在三个独立的UI框架中有三个独立的命名空间中的Button类。)

It also means that occasionally you may want or need the same class name in multiple packages - where the package name effectively provides the context. Now if you're working in a single codebase for a single application, that's usually something to try to avoid - but if you've got a large codebase where many different applications use many different parts of it, that may be better than trying to have a unique name for every single class. (A typical example of this is in user interface code - just looking in the .NET libraries, there's a "Button" class in three separate namespaces, for three separate UI frameworks.)

这篇关于如何使用Java包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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