Download presentation
Presentation is loading. Please wait.
Published byDaisy Gardner Modified over 8 years ago
1
成都信息工程学院 计算机系 1 Java 程序设计 Java Programming Fall, 2010
2
Chapter 8 Nested Classes & Interfaces, & Exceptions 2 Contents Package
3
Chapter 8 Nested Classes & Interfaces, & Exceptions 3 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.
4
Chapter 8 Nested Classes & Interfaces, & Exceptions 4 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
5
Chapter 8 Nested Classes & Interfaces, & Exceptions 5 Java Packages Package: A collection of related classes and/or interfaces. The classes stored in each directory form a package. 1. System-defined packages - JRE System Library 2. User-defined packages
6
Chapter 8 Nested Classes & Interfaces, & Exceptions 6
7
7
8
8 Package NameContents java.appletClasses for implementing applets java.awtClasses for graphics, windows, and GUI ’ s java.awt.eventClasses supporting AWT event handling java.awt.imageClasses for image handling java.awt.peerInterface definitions s for platform independent graphical user interfaces (GUI ’ s) java.ioClasses for input and output java.langBasic language classes like Math (always available in any Java program) java.netClasses for networking java.utilUseful auxiliary classes like Date Some Predefined Java Packages
9
9 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). … Graphics Font java Image awt lang “java” Package containing “lang”, “awt”,.. packages; Can also contain classes. awt Package containing classes Classes containing methods
10
Chapter 8 Nested Classes & Interfaces, & Exceptions 10 Examples of Java Packages java.lang package: Object Math The wrapper classes String StringBuffer
11
11 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
12
Chapter 8 Nested Classes & Interfaces, & Exceptions 12 Import Examples This code java.util.Date d = new java.util.Date(); java.awt.Point p = new java.awt.Point(1,2); java.awt.Button b = new java.awt.Button(); Can be abbreviated import java.util.date; import java.awt.*; … Date d = new Date(); Point p = new Point(1,2); Button b = new Button();
13
13 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 { … }
14
Chapter 8 Nested Classes & Interfaces, & Exceptions 14 Creating Sub-Packages As packages in Java are organised hierarchically, sub- packages 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 { … }
15
Chapter 8 Nested Classes & Interfaces, & Exceptions 15 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.
16
Chapter 8 Nested Classes & Interfaces, & Exceptions 16 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.
17
Chapter 8 Nested Classes & Interfaces, & Exceptions 17 Visibility Modifiers
18
Chapter 8 Nested Classes & Interfaces, & Exceptions 18 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"); }
19
Chapter 8 Nested Classes & Interfaces, & Exceptions 19 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(); }
20
20 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(); }
21
21 创建包示例 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!"); }
22
22 创建包示例 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!"); }
23
23 包的引用示例: 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(); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.