Packages

advertisement
Java程序设计
Java Programming
Fall, 2010
成都信息工程学院 计算机系
1
Contents

Package
Chapter 8 Nested Classes & Interfaces, & Exceptions
2
Java Program Organization

Java program


1 or more Java source files
Source file


1 or more class and/or interface declarations.
If a class/interface is public, the source file must use the
same (base) name



So, only one public class/interface per source file
Can have non-public classes! We will discuss later
Packages

When a program is large, its classes can be organized
hierarchically into packages.
Chapter 8 Nested Classes & Interfaces, & Exceptions
3
Package(包)

Why?



A large program may consists of hundreds of classes
(Example: 800 in one current project with NASA).
Packages enable a programmer organize the code into
smaller logically related units.
Every class is part of some package.

If you do not specify a package a class becomes part
of the default package
Chapter 8 Nested Classes & Interfaces, & Exceptions
4
Java Packages


Package: A collection of related classes and/or
interfaces.
The classes stored in each directory form a
package.
1.
2.
System-defined packages - JRE System Library
User-defined packages
Chapter 8 Nested Classes & Interfaces, & Exceptions
5
Chapter 8 Nested Classes & Interfaces, & Exceptions
6
Chapter 8 Nested Classes & Interfaces, & Exceptions
7
Some Predefined Java Packages
Package Name
Contents
java.applet
Classes for implementing applets
java.awt
Classes for graphics, windows, and GUI’s
java.awt.event
Classes supporting AWT event handling
java.awt.image
Classes for image handling
java.awt.peer
Interface definitions s for platform independent
graphical user interfaces (GUI’s)
java.io
Classes for input and output
java.lang
Basic language classes like Math
(always available in any Java program)
java.net
Classes for networking
java.util
Useful auxiliary classes like Date
Chapter 8 Nested Classes & Interfaces, & Exceptions
8
Java Packages
• The packages are organised in a hierarchical(分层)
structure. For example, a package named “java”
contains the package “awt”, which in turn contains
various classes required for implementing GUI
(graphical user interface).
java
“java” Package containing
“lang”, “awt”,.. packages;
Can also contain classes.
lang
awt
Graphics
Font
Image
…
awt Package containing
classes
Classes containing
methods
9
Examples of Java Packages
• java.lang package:





Object
Math
The wrapper classes
String
StringBuffer
Chapter 8 Nested Classes & Interfaces, & Exceptions
10
Using System Packages
1. Using a fully qualified component name:
x = java.lang.Math.sqrt(3);
2. Using an import statement:
import packagename.*;
// to allow unqualified references to
// all package classes
import packagename.class_name;
// to allow unqualified references to
// a particular package class
11
Import Examples

This code

java.util.Date d =
new java.util.Date();
java.awt.Point p =
new java.awt.Point(1,2);
Can be abbreviated
import java.util.date;
import java.awt.*;
…
Date d = new Date();
Point p = new Point(1,2);
Button b = new Button();
java.awt.Button b =
new java.awt.Button();
Chapter 8 Nested Classes & Interfaces, & Exceptions
12
Defining Packages
• To define: add package statement to specify
package containing the classes of a file

Must be first non-comment statement in file
package mypackage; //must be first line
…
//myClass is part of mypackage
public class myClass { … }
13
Creating Sub-Packages


As packages in Java are organised hierarchically, subpackages can be created as follows:
package myPackage.secondPakage.thirdPackage;
Store “thirdPackage” in a subdirectory named
“myPackage\secondPackage”.
package mypackage.mysubpackage;
…
//myClass2 is part of mysubpackage, which is
//within mypackage
public class myClass2 { … }
Chapter 8 Nested Classes & Interfaces, & Exceptions
14
User-Defined Packages

Access(包的访问)


Classes defined within the same package can access
one another more easily (no need for imports, fully
qualified names);
Some classes, object fields only accessible to classes in
same package.
Chapter 8 Nested Classes & Interfaces, & Exceptions
15
Visibility Rules and Packages



Instance variables without explicitly declared visibility have
package visibility;
Instance variables, static variables, instance methods, and
static methods with package visibility are only visible to
methods defined in classes belonging to the same package;
Classes not explicitly declared public are not visible outside
the package.
Chapter 8 Nested Classes & Interfaces, & Exceptions
16
Visibility Modifiers
Accessible to:
public
protected
Package
(default)
private
Same Class
Yes
Yes
Yes
Yes
Class in package
Yes
Yes
Yes
No
Subclass in
different package
Yes
Yes
No
No
Non-subclass
different package
Yes
No
No
No
Chapter 8 Nested Classes & Interfaces, & Exceptions
17
Using a Package

Let us store the code listing below in a file named
“ClassA.java” within subdirectory named “myPackage”
within the current directory (say “abc”).
package myPackage;
public class ClassA {
// class body
public void display() {
System.out.println("Hello, I am ClassA");
}
}
Chapter 8 Nested Classes & Interfaces, & Exceptions
18
Using a Package

Within the current directory (“abc”) store the following
code in a file named “ClassX.java”
import myPackage.ClassA;
public class ClassX {
public static void main(String args[]) {
ClassA objA = new ClassA();
objA.display();
}
}
Chapter 8 Nested Classes & Interfaces, & Exceptions
19
package myPackage;
public class ClassA {
public void display(){
System.out.println("Hello, I am ClassA");
}
}
import myPackage.ClassA;
public class ClassX {
public static void main(String args[]){
ClassA objA = new ClassA();
objA.display();
}
}
20
创建包示例 X1.java
package card; //创建包
public class X1{
int x, y;
public X1(int i, int j){
this.x=i;
this.y=j;
System.out.println("x="+x+" "+"y="+y);
}
public void show(){
System.out.println("this class is a X1!");
}
}
21
创建包示例 X2.java
package card; //创建包
public class X2 {
int m,n;
public X2(int i,int j) {
this.m=i;
this.n=j;
System.out.println("m="+m+" "+"n="+n);
}
public void show(){
System.out.println("this class is a X2!");
}
}
22
包的引用示例:
Pack.java不在card包内,但和card包在同一个目录中。
import card.X1; //导入包card中的X1类
import card.X2; //导入包card中的X2类
//import card.*;
public class Pack {
public static void main(String args[]){
X1 aa=new X1(4,5);
aa.show();
X2 bb=new X2(10,20);
bb.show();
}
23
Download