Java封装 [英] Package in Java
问题描述
我们何时真正使用package关键字?是什么意思?
When do we actually use the package keyword? What does it mean?
假设我编写了以下代码:
Suppose I write the following code:
package masnun;
public class masnun{
public static void main(String args[]) {
System.out.println("Hello maSnun!");
}
}
此包装的作用是什么?我得到了一个无法运行的masnun.class文件.我是Java新手.有人可以解释一下吗?
What does this package thing do? I get a masnun.class file that doesn't run. I am new to Java. Can somebody please explain?
谢谢
推荐答案
由于我不喜欢这些其他答案,因此我将自己编写.
As I'm not a fan of these other answers, I'll write my own.
真实世界的示例:
将包"作为java类引用另一个的简单方法.
Think of a "package" as an easy way for a java class to reference another.
假设我的阁楼里有这个大盒子.我有计算器,指南针,量角器等.我可以将此框标记为 MathTools
.
Let's say I have this big box in my attic. I have a calculator, compass, protractor, etc. I can label this box MathTools
.
另一个示例是拍摄所有图片并将其放入文档的 Pictures
文件夹中.从那里,您可以将它们拆分为 Spring Break 2009
或 [在此处插入名称]的聚会
.
Another example would be taking all your pictures and putting them in the Pictures
folder in your documents. From there, you could split them into Spring Break 2009
or [Insert Name Here]'s Party
.
这与Java有何关系?好吧,让我们看一下 java.util
包(您可以通过 import java.util.*;
引用它.您有ArrayLists,Strings,Random等.在大多数Java程序中使用(如果需要,可以使用常见的实用程序".)它们都整齐地组织在同一个程序包中,以便程序员可以轻松地引用它们( import java.util.*;
).
How does this relate to Java? Well, let's look at the java.util
package (you can reference this with import java.util.*;
. You have ArrayLists, Strings, Random, etc. which are used in most Java programs (common "utilities", if you prefer). There are all neatly organized into the same package, so that programmers can easily reference them (import java.util.*;
).
简易申请:
假设我们可以在 C:/Program Files/Java Project/my/proj/
中找到所有文件到小型骰子模拟器中(您的文件可能不存在此文件电脑,但假装一会儿.
Let's assume that we can find all the files to a small dice simulator in C:/Program Files/Java Project/my/proj/
(it's likely that this file doesn't exist on your computer, but just pretend for a moment).
您有3个文件: Main.java
, Dice.java
和 DiceRoller.java
.所有这些都显示如下:
You have 3 files: Main.java
, Dice.java
, and DiceRoller.java
. All of which are shown below:
.
"C:/ProgramFiles/Java Project/my/proj/main/ Main.java
":
"C:/ProgramFiles/Java Project/my/proj/main/Main.java
":
package my.proj.main;
import my.proj.sims.Dice;
public class Main
{
public static void main(String[] args)
{
DiceRoller roller = new DiceRoller();
roller.rollAndShow(4);
}
}
"C:/ProgramFiles/Java Project/my/proj/sims/ Dice.java
":
"C:/ProgramFiles/Java Project/my/proj/sims/Dice.java
":
package my.proj.sims;
import java.util.Random; // I used the Random class, but you can also use the Math class if you prefer (java.lang.Math)
public class Dice
{
public Dice()
{
}
public int roll()
{
Random rand = new Random();
return rand.nextInt(6) + 1; // Rolls a random number 1-6
}
}
"C:/ProgramFiles/Java Project/my/proj/sims/ DiceRoller.java
":
"C:/ProgramFiles/Java Project/my/proj/sims/DiceRoller.java
":
package my.proj.sims;
public class DiceRoller
{
public DiceRoller ()
{
}
// Rolls and prints the result of 'n' number of rolls
public void rollAndShow(int n)
{
Dice dice = new Dice();
for (int i = 0; i < n; i++)
{
System.out.println(dice.roll()); // You should never use S.o.p in a method - it's bad practice, but it's easier this way if you don't yet understand the concept of objects
}
}
}
.
注意事项:
-
Main.java
打包到my.proj.main
中 -
Dice.java
被打包到my.proj.sims
中 -
Main.java
需要导入my.proj.sims.Dice
以便创建Dice
对象并使用其方法,因为在与Dice.java
不同的包中. -
DiceRoller.java
不需要导入my.proj.sims.Dice
,因为它与Dice.java
处于同一软件包中然后编译器会自动将两者关联.
Main.java
is packaged intomy.proj.main
Dice.java
is packaged intomy.proj.sims
Main.java
needs to importmy.proj.sims.Dice
in order to create aDice
object and use its methods because it's in a different package fromDice.java
.DiceRoller.java
does not need to importmy.proj.sims.Dice
because it is in the same package asDice.java
and the compiler will automatically associate the two.
.
导入
是用于将类的功能加载到当前文件中的命令.例如,查看 Dice.java
.为了创建具有方法 nextInt()
的 Random
对象,它需要从 java.util.* <中导入Random类./code>包.
Import
is a command to load the functionality of a class into the current file. Look at Dice.java
, for example. In order for it to create a Random
object, which has the method nextInt()
, it needs to import the Random class from the java.util.*
package.
.
您可能会注意到,有些人宁愿使用 java.util.*
而不是 java.util.Random
, java.util.ArrayList
等. *
本质上是指 java.util
中的任何类.运行 import java.util.*
将导入Random,String,ArrayList等类.
You might notice that some people would prefer to use java.util.*
instead of java.util.Random
, java.util.ArrayList
, etc. What the *
essentially means is any class within java.util
. Running import java.util.*
will import the Random, String, ArrayList, etc. classes.
.
希望这可以清除一切.如果这对您有帮助,请考虑投票:)
Hope this clears things up. Please consider upvoting if this has helped you :)
这篇关于Java封装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!