包(Package)由一组类(class)和界面(interface)组成。它是管理大型名字空间,避免名字冲突的工具。每一个类和界面的名字都包含在某个包中。按照一般的习惯,它的名字是由“.”号分隔的单词构成,第一个单词通常是开发这个包的组织的名称。定义一个编译单元的包编译单元的包由package语句定义。如果使用package语句,编译单元的第一行必须无空格,也无注释。其格式如下:packagepackageName;若编译单元无package语句,则该单元被置于一个缺省的无名的包中。使用其它包中的类和界面在Java语言里提一个包可以使用另一个包中类和界面的定义和实现的机制。用import关键词来标明来自其它包中的类。一个编译单元可以自动把指定的类和界面输入到它自己的包中。在一个包中的代码可以有两种方式来定义来自其它包中的类和界面:*在每个引用的类和界面前面给出它们所在的包的名字;//前缀包名法acme.project.FooBarobj=newacme.project.FooBar();*使用import语句,引入一个类或一个界面,或包含它们的包。引入的类和界面的名字在当前的名字空间可用。引入一个包时,则该包所有的公有类和界面均可用。其形式如下://从acme.project引入所有类importacme.project.*;这个语句表示acme.project中所有的公有类被引入当前包。以下语句从acme.project包中进入一个类Employec_List。//从acme.project而引入Employee_Listimportacme.project.Employee_list;Employee_Listobj=newEmployee_List();在使用一个外部类或界面时,必须要声明该类或界面所在的包,否则会产生编译错误。import(引用)类包(classpackage)用import关键词调入,指定package名字如路径和类名,用*匹配符可以调入多于一个类名。importjava.Date;importjava.awt.*;如果java源文件不包含package,它放在缺省的无名package。这与源文件同目录,类可以这样引入:importMyClass。Java系统包:Java语言提了一个包含窗口工具箱,实用程序,一般I/O,工具和网络功能的包。java.applet这个包包含量了一设计applet的类,用一个类Applet和三个接口.AppletContext;AppletStub;和AudioClip.java.awt另一个窗口工具箱包.awt,包含了一产生装饰物和GUI成员的类。这个package包括:Button,Checkbox,Choice,Component,Graphics,Menu,Pane1,TextArea和TextField。java.ioI/Opackage包含文件输入/输出类,FileInputStream和FileOutputStream.java.lang这个包包含Java语言类,包含:对象,线程,异常出口,系统,整数,原点,数学,字符等。java.net这个类支持TCP/IP网络协议,并包含Socket类,URL和URL相联系的类。java.util这个类包含一程序的同步类,它包含Date,Dictionary类等。
自创建类Threader.class(由Threader.java编译而来),
在文件CreatRace.java中使用 import Threader;
这些文件都存储在c:\test中,
编译报错如下:C:\test>javac CreatRace.java
CreatRace.java:4: '.' expected
import Threader;
请问该怎么办?
1.每个文件加入package yourPackageName;2.然后,如果调用Threader中的方法,是不是要写上.*呀?try it !goodluck!
1。CreatRace.java
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Frame;
import Threader;
public class CreatRace extends java.applet.Applet implements Runnable{
Threader theRacers[];
static int racerCount = 3;
Thread theThreads[];
Thread thisThread;
static boolean inApplet =true;
int numberofThreadsAtStart; public void init(){
//we will use this later to see if all our Threads have died.
numberofThreadsAtStart = Thread.activeCount(); //Specify the layout.We vill be adding all of the racers one on top of the other setLayout(new GridLayout(racerCount,1)); //Specify the numberof racers in this race , and make the arrays for the
//Threaders and the actual threads the proper size. theRacers = new Threader[racerCount];
theThreads = new Thread[racerCount]; //Great a new Thread for each racer,and add it to the panel.
for(int x=0;x<racerCount;x++){
theRacers[x] = new Threader("Racer #"+x);
theRacers[x].setSize(getSize().width,getSize().height/racerCount);
add(theRacers[x]);
theThreads[x] = new Thread(theRacers[x]);
}
}
public void start(){
//start all of the racing threads
for(int x=0;x<racerCount;x++)
theThreads[x].start();
//Create a thread of our own.We will use this to monitor the stae of
//the racers and determine when we should quit altogether.
thisThread = new Thread(this);
thisThread.start();
} public void stop(){
for(int x = 0;x < theRacers.length;x++){
theThreads[x].stop();
}
} public void run(){
//Loop around until all of the racers have finished the race.
while(Thread.activeCount() > numberofThreadsAtStart+2){
try{
thisThread.sleep(100);
}catch(InterruptedException e){
System.out.println("This Thread was interupted");
}
} //Once the race is done, end the program.
if(inApplet){
stop();
destroy();
}
else
System.exit(0);
} public static void main(String argv[]){
inApplet = false; //Check to see if the number of racers has been specified on the command line.
if(argv.length>0)
racerCount = Integer.parseInt(argv[0]); //Create a new frame and place the race in it.
Frame theFrame = new Frame("The Great Thread Race");
CreatRace theRace = new CreatRace();
theFrame.setSize(400,200);
theFrame.add("Center",theRace);
theFrame.show();
theRace.init();
theFrame.pack();
theRace.start(); }
}2。Threader.java
import java.awt.Graphics;
import java.awt.Color;public class Threader extends java.awt.Canvas implements Runnable{
int myPosition = 0;
String myName;
int numberofSteps = 600;
boolean keepRunning = true; //Constructor for a Threader.We need th know our name when we
//create the Threader.
public Threader(String inName){
myName = new String(inName);
} public synchronized void paint(Graphics g){
//Draw a line for the 'racing line'.
g.setColor(Color.black);
g.drawLine(0,getSize().height/2,getSize().width,getSize().height/2); //Listing 13.2 Continued
//Draw the round racer.
g.setColor(Color.yellow);
g.fillOval((myPosition*getSize().width/numberofSteps),0,15,getSize().height);
} public void run(){
//Loop until we have finished the race.
while((myPosition < numberofSteps)&&keepRunning){
//Move ahead one position.
myPosition++;
repaint(); //Put ourselves to sleep so the paint thread can get around to painting.
try{
Thread.currentThread().sleep(10);
}catch(Exception e){System.out.println("Exception on sleep");}
}
System.out.println("Threader:"+myName+"has finished the race");
}
}是书上的例子,如果我把Threader类去掉public写入CreatRace(这样就不用import Threader了),程序能够很好的执行。请大虾再帮忙看看。
在每个你自制的文件头上写入package xxx
import java.awt.GridLayout;
import java.awt.Frame;
import Threader;
//Threader是同目录下的public类名,可以直接被此类使用,不用import,你可以注释掉这行,直接编译此文件,一切ok!
如果你要用import的形式,那就在Threader.java开始处写上一行:package xxx;
这样你在CreatRace.java中如是写:
import xxx.Threader;
但是要保证你的这两个源文件放在同一目录下,肯定OK!
//方式1:
//Threader.java
package xxx;
import java.awt.Graphics;
import java.awt.Color;public class Threader extends java.awt.Canvas implements Runnable{
int myPosition = 0;
String myName;
int numberofSteps = 600;
boolean keepRunning = true; //Constructor for a Threader.We need th know our name when we
//create the Threader.
public Threader(String inName){
myName = new String(inName);
} public synchronized void paint(Graphics g){
//Draw a line for the 'racing line'.
g.setColor(Color.black);
g.drawLine(0,getSize().height/2,getSize().width,getSize().height/2); //Listing 13.2 Continued
//Draw the round racer.
g.setColor(Color.yellow);
g.fillOval((myPosition*getSize().width/numberofSteps),0,15,getSize().height);
} public void run(){
//Loop until we have finished the race.
while((myPosition < numberofSteps)&&keepRunning){
//Move ahead one position.
myPosition++;
repaint(); //Put ourselves to sleep so the paint thread can get around to painting.
try{
Thread.currentThread().sleep(10);
}catch(Exception e){System.out.println("Exception on sleep");}
}
System.out.println("Threader:"+myName+"has finished the race");
}
}//////////////////////////////////////////////////////////////////////////////
//CreatRace.java
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Frame;
import xxx.Threader;public class CreatRace extends java.applet.Applet implements Runnable{
Threader theRacers[];
static int racerCount = 3;
Thread theThreads[];
Thread thisThread;
static boolean inApplet =true;
int numberofThreadsAtStart; public void init(){
//we will use this later to see if all our Threads have died.
numberofThreadsAtStart = Thread.activeCount(); //Specify the layout.We vill be adding all of the racers one on top of the other setLayout(new GridLayout(racerCount,1)); //Specify the numberof racers in this race , and make the arrays for the
//Threaders and the actual threads the proper size. theRacers = new Threader[racerCount];
theThreads = new Thread[racerCount]; //Great a new Thread for each racer,and add it to the panel.
for(int x=0;x<racerCount;x++){
theRacers[x] = new Threader("Racer #"+x);
theRacers[x].setSize(getSize().width,getSize().height/racerCount);
add(theRacers[x]);
theThreads[x] = new Thread(theRacers[x]);
}
}
public void start(){
//start all of the racing threads
for(int x=0;x<racerCount;x++)
theThreads[x].start();
//Create a thread of our own.We will use this to monitor the stae of
//the racers and determine when we should quit altogether.
thisThread = new Thread(this);
thisThread.start();
} public void stop(){
for(int x = 0;x < theRacers.length;x++){
theThreads[x].stop();
}
} public void run(){
//Loop around until all of the racers have finished the race.
while(Thread.activeCount() > numberofThreadsAtStart+2){
try{
thisThread.sleep(100);
}catch(InterruptedException e){
System.out.println("This Thread was interupted");
}
} //Once the race is done, end the program.
if(inApplet){
stop();
destroy();
}
else
System.exit(0);
} public static void main(String argv[]){
inApplet = false; //Check to see if the number of racers has been specified on the command line.
if(argv.length>0)
racerCount = Integer.parseInt(argv[0]); //Create a new frame and place the race in it.
Frame theFrame = new Frame("The Great Thread Race");
CreatRace theRace = new CreatRace();
theFrame.setSize(400,200);
theFrame.add("Center",theRace);
theFrame.show();
theRace.init();
theFrame.pack();
theRace.start(); }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//方式2:
//Threader.java
import java.awt.Graphics;
import java.awt.Color;public class Threader extends java.awt.Canvas implements Runnable{
int myPosition = 0;
String myName;
int numberofSteps = 600;
boolean keepRunning = true; //Constructor for a Threader.We need th know our name when we
//create the Threader.
public Threader(String inName){
myName = new String(inName);
} public synchronized void paint(Graphics g){
//Draw a line for the 'racing line'.
g.setColor(Color.black);
g.drawLine(0,getSize().height/2,getSize().width,getSize().height/2); //Listing 13.2 Continued
//Draw the round racer.
g.setColor(Color.yellow);
g.fillOval((myPosition*getSize().width/numberofSteps),0,15,getSize().height);
} public void run(){
//Loop until we have finished the race.
while((myPosition < numberofSteps)&&keepRunning){
//Move ahead one position.
myPosition++;
repaint(); //Put ourselves to sleep so the paint thread can get around to painting.
try{
Thread.currentThread().sleep(10);
}catch(Exception e){System.out.println("Exception on sleep");}
}
System.out.println("Threader:"+myName+"has finished the race");
}
}//////////////////////////////////////////////////////////////////////////////
//CreatRace.java
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Frame;public class CreatRace extends java.applet.Applet implements Runnable{
Threader theRacers[];
static int racerCount = 3;
Thread theThreads[];
Thread thisThread;
static boolean inApplet =true;
int numberofThreadsAtStart; public void init(){
//we will use this later to see if all our Threads have died.
numberofThreadsAtStart = Thread.activeCount(); //Specify the layout.We vill be adding all of the racers one on top of the other setLayout(new GridLayout(racerCount,1)); //Specify the numberof racers in this race , and make the arrays for the
//Threaders and the actual threads the proper size. theRacers = new Threader[racerCount];
theThreads = new Thread[racerCount]; //Great a new Thread for each racer,and add it to the panel.
for(int x=0;x<racerCount;x++){
theRacers[x] = new Threader("Racer #"+x);
theRacers[x].setSize(getSize().width,getSize().height/racerCount);
add(theRacers[x]);
theThreads[x] = new Thread(theRacers[x]);
}
}
public void start(){
//start all of the racing threads
for(int x=0;x<racerCount;x++)
theThreads[x].start();
//Create a thread of our own.We will use this to monitor the stae of
//the racers and determine when we should quit altogether.
thisThread = new Thread(this);
thisThread.start();
} public void stop(){
for(int x = 0;x < theRacers.length;x++){
theThreads[x].stop();
}
} public void run(){
//Loop around until all of the racers have finished the race.
while(Thread.activeCount() > numberofThreadsAtStart+2){
try{
thisThread.sleep(100);
}catch(InterruptedException e){
System.out.println("This Thread was interupted");
}
} //Once the race is done, end the program.
if(inApplet){
stop();
destroy();
}
else
System.exit(0);
} public static void main(String argv[]){
inApplet = false; //Check to see if the number of racers has been specified on the command line.
if(argv.length>0)
racerCount = Integer.parseInt(argv[0]); //Create a new frame and place the race in it.
Frame theFrame = new Frame("The Great Thread Race");
CreatRace theRace = new CreatRace();
theFrame.setSize(400,200);
theFrame.add("Center",theRace);
theFrame.show();
theRace.init();
theFrame.pack();
theRace.start(); }
}
/////////////////////////////////////////////////////////////////////////
的类有效,package的概念就是相当于名字空间,比如相同的名字在不同的
包中,可以互不干扰的使用,如果没有包的概念则很难保证类名不重,所以
往往人们都习惯用域名做包名,因为域名是全世界唯一的!
import thrdPkg.Threader;
^
CreatRace.java:7: cannot access Threader
bad class file: .\Threader.class
class file contains wrong class: thrdPkg.Threader
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
Threader theRacers[];为什么找不到这个包呢?
还有,你有没有严格的按照我的来?两个源文件要放在相同的目录下编译?
最好把你的源代码贴上来,还有你用的什么方式编译的:命令行还是IDE?如果是IDE,用的哪个?
package thrdPkg;
import java.awt.Graphics;
import java.awt.Color;public class Threader extends java.awt.Canvas implements Runnable{
int myPosition = 0;
String myName;
int numberofSteps = 600;
boolean keepRunning = true; //Constructor for a Threader.We need th know our name when we
//create the Threader.
public Threader(String inName){
myName = new String(inName);
} public synchronized void paint(Graphics g){
//Draw a line for the 'racing line'.
g.setColor(Color.black);
g.drawLine(0,getSize().height/2,getSize().width,getSize().height/2); //Listing 13.2 Continued
//Draw the round racer.
g.setColor(Color.yellow);
g.fillOval((myPosition*getSize().width/numberofSteps),0,15,getSize().height);
}
public void stop(){
keepRunning = false;
}
public void run(){
//Loop until we have finished the race.
while((myPosition < numberofSteps)&&keepRunning){
//Move ahead one position.
myPosition++;
repaint(); //Put ourselves to sleep so the paint thread can get around to painting.
try{
Thread.currentThread().sleep(10);
}catch(Exception e){System.out.println("Exception on sleep");}
}
System.out.println("Threader:"+myName+"has finished the race");
}
}//CreatRace.java
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Frame;
import thrdPkg.Threader;public class CreatRace extends java.applet.Applet implements Runnable{
Threader theRacers[];
static int racerCount = 3;
Thread theThreads[];
Thread thisThread;
static boolean inApplet =true;
int numberofThreadsAtStart; public void init(){
//we will use this later to see if all our Threads have died.
numberofThreadsAtStart = Thread.activeCount(); //Specify the layout.We vill be adding all of the racers one on top of the other setLayout(new GridLayout(racerCount,1)); //Specify the numberof racers in this race , and make the arrays for the
//Threaders and the actual threads the proper size. theRacers = new Threader[racerCount];
theThreads = new Thread[racerCount]; //Great a new Thread for each racer,and add it to the panel.
for(int x=0;x<racerCount;x++){
theRacers[x] = new Threader("Racer #"+x);
theRacers[x].setSize(getSize().width,getSize().height/racerCount);
add(theRacers[x]);
theThreads[x] = new Thread(theRacers[x]);
theThreads[x].setPriority(Thread.MIN_PRIORITY + x);
}
}
public void start(){
//start all of the racing threads
for(int x=0;x<racerCount;x++)
theThreads[x].start();
//Create a thread of our own.We will use this to monitor the stae of
//the racers and determine when we should quit altogether.
thisThread = new Thread(this);
thisThread.start();
} public void stop(){
for(int x = 0;x < theRacers.length;x++){
theRacers[x].stop();
}
} public void run(){
//Loop around until all of the racers have finished the race.
while(Thread.activeCount() > numberofThreadsAtStart+2){
try{
thisThread.sleep(100);
}catch(InterruptedException e){
System.out.println("This Thread was interupted");
}
} //Once the race is done, end the program.
if(inApplet){
stop();
destroy();
}
else
System.exit(0);
} public static void main(String argv[]){
inApplet = false; //Check to see if the number of racers has been specified on the command line.
if(argv.length>0)
racerCount = Integer.parseInt(argv[0]); //Create a new frame and place the race in it.
Frame theFrame = new Frame("The Great Thread Race");
CreatRace theRace = new CreatRace();
theFrame.setSize(400,200);
theFrame.add("Center",theRace);
theFrame.show();
theRace.init();
theFrame.pack();
theRace.start(); }
}
两个源文件均在c:\test目录下,使用命令行编译(jdk1.4)。
环境变量设置如下:
PATH=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\WbemC:\j2sdk1.4.1_02\binCLASSPATH=.;C:\j2sdk1.4.1_02\lib\dt.jar;C:\j2sdk1.4.1_02\lib\tools.jar;C:\j2sdk1
.4.1_02\lib\htmlconverter.jar;C:\j2sdk1.4.1_02\lib\tools.jar;C:\j2sdk1.4.1_02\li
b;c:\test
PATH=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\WbemC:\j2sdk1.4.1_02\bin
+
+
这里!对了,你如果用命令行编译的话,在原来命令行的末尾添加这样的参数:
-d . (注意这里的.,表示当前目录,)比如我的这两个文件放在F:\
那么应该这样:
F:\javac Threader.java -d .
F:\javac CreatRace.java
F:\java CreatRace