简体   繁体   English

java包只有一个公共类吗?

[英]Should a java package have only one public class?

I tired creating a package with one public class and one with multiple public classes and then import them through another package and both times the program worked. 我厌倦了创建一个包含一个公共类的包和一个包含多个公共类的包,然后通过另一个包导入它们,这两次都是程序运行的。 I understand the difference, in the first i can only use the methods of the one public class, but that class can use other classes in its package, and when more classes are public i can directly access all those classes through the alien package. 我理解差异,在第一个我只能使用一个公共类的方法,但该类可以在其包中使用其他类,当更多类是公共的时,我可以通过外包直接访问所有这些类。

Now when we import java.util.Scanner, and we import java.util.ArrayList isn't this the same package? 现在当我们导入java.util.Scanner时,我们导入java.util.ArrayList不是这个相同的包吗? When we import our package with one public class it only shows the one public. 当我们用一个公共类导入我们的包时,它只显示一个公共类。 That means that java.util has multiple public classes, or? 这意味着java.util有多个公共类,或者?

Also what does that java. 那个java也是什么呢。 mean, does it make the difference between my package and java library packages? 是的,它是否区分我的包和Java库包?

Yes, a package can have multiple public classes. 是的,一个包可以有多个public类。

Creating a new package for every public class would be extremely cumbersome and provide no overview at all. 为每个public类创建一个新包将非常麻烦,根本不提供任何概述。

java. are just the standard libraries that come with the JDK/JRE. 只是JDK / JRE附带的标准库。

Packages are really just like folders on your system. 软件包就像系统中的文件夹一样。 You can have multiple files in a folder without a problem. 您可以在文件夹中放置多个文件而不会出现问题。

Typically packages are structured in a way that they combine related classes to make it easier for the developer. 通常,包的结构使它们组合相关的类以使开发人员更容易。 If you want to have no packages at all, or you want a new package for every class: it doesn't matter. 如果您想要根本没有包,或者您想为每个类创建一个新包:它没关系。 You'll be your own worst enemy, but it is surely possible. 你将成为自己最大的敌人,但肯定是可能的。

import java.util.ArrayList;

means: let me use the class ArrayList , which is in the package java.util , by only typing ArrayList and not java.util.ArrayList . 意思是:让我通过输入ArrayList而不是java.util.ArrayListjava.util的类ArrayList

java.util is the name of the package, nothing else. java.util是包的名称,没有别的。 It's where the Java developers chose to store the ArrayList class. 这是Java开发人员选择存储ArrayList类的地方。 It's not different from any other package name, except you're not allowed to use the java package for your own classes, because it's reserved for standard Java classes. 它与任何其他包名称没有什么不同,除非您不允许将java包用于您自己的类,因为它是为标准Java类保留的。

import java.util.*;

means: let me use all the classes of the package java.util ( ArrayList , LinkedList , Collection , etc.) by typing their simple name ( ArrayList , LinkedList , Collection , etc.) instead of their fully qualified name ( java.util.ArrayList , java.util.LinkedList , java.util.Collection , etc.) 意思是:让我使用包java.utilArrayListLinkedListCollection等)的所有类,通过键入它们的简单名称( ArrayListLinkedListCollection等)而不是它们的完全限定名( java.util.ArrayListjava.util.LinkedListjava.util.Collection等)

All these classes are public (as documented in their javadoc). 所有这些类都是公开的(如javadoc中所述)。 If they weren't public, you wouldn't be able to use them in your own classes. 如果它们不公开,您将无法在自己的课程中使用它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java 程序只有一个公共 class 应该主要方法只是该公共 class 的一部分,然后是该程序的任何其他 ZA2F2ED4F8DCEBC2CBBDZC21? - java program have only one public class should main method only be part of that public class then any other class of that program? java.lang.Exception错误:测试类应该恰好具有一个公共构造函数 - ERROR with java.lang.Exception: Test class should have exactly one public constructor java.lang.Exception: 测试类应该只有一个公共构造函数 - java.lang.Exception: Test class should have exactly one public constructor java.lang.Exception:测试类应该只有一个公共零参数构造函数: - java.lang.Exception: Test class should have exactly one public zero-argument constructor: JUnit 类应该只有一个公共构造函数 - JUnit class should have exactly one public constructor 我可以在一个 java 包中定义多个公共类吗 - can i define more than one public class in a java package junit java 的参数化测试构造函数错误消息:测试 class 应该只有一个公共零参数构造函数 - parameterized test constructor of junit java error message: Test class should have exactly one public zero-argument constructor 如何使一个Java类仅对包中的另一个类可用 - How to make a java class only available to one other class in a package 类中的公共静态方法仅具有包可见性? - public static methods in class with only package visiblility? 测试类应该只有一个公共零参数构造函数 - Test class should have exactly one public zero-argument constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM