1 / 23

Java 程序设计

Java 程序设计. Java Programming Fall, 2010. Contents. Package. 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

riona
Download Presentation

Java 程序设计

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java程序设计 Java Programming Fall, 2010 成都信息工程学院 计算机系

  2. Contents • Package 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. 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 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. • System-defined packages - JRE System Library • User-defined packages Chapter 8 Nested Classes & Interfaces, & Exceptions

  6. Chapter 8 Nested Classes & Interfaces, & Exceptions

  7. Chapter 8 Nested Classes & Interfaces, & Exceptions

  8. Some Predefined Java Packages Chapter 8 Nested Classes & Interfaces, & Exceptions

  9. java “java” Package containing “lang”, “awt”,.. packages; Can also contain classes. lang awt Graphics awt Package containing classes Font Classes containing methods Image 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). …

  10. Examples of Java Packages • java.langpackage: • Object • Math • The wrapper classes • String • StringBuffer Chapter 8 Nested Classes & Interfaces, & Exceptions

  11. Using System Packages • Using a fully qualified component name: x = java.lang.Math.sqrt(3); • Using an importstatement: import packagename.*; // to allow unqualified references to // all package classes import packagename.class_name; // to allow unqualified references to // a particular package class

  12. 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(); Import Examples Chapter 8 Nested Classes & Interfaces, & Exceptions

  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. 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 { … } 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. 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. Chapter 8 Nested Classes & Interfaces, & Exceptions

  17. Visibility Modifiers 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"); } } 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(); } } Chapter 8 Nested Classes & Interfaces, & Exceptions

  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. 创建包示例 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. 创建包示例 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. 包的引用示例:Pack.java不在card包内,但和card包在同一个目录中。包的引用示例: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(); } }

More Related