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

Slides:



Advertisements
Similar presentations
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
Advertisements

Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy.
ICS 201 Inheritance Introduction to Computer Science
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
PACKAGES. PACKAGES IN JAVA A package is a collection of related classes and interfaces in Java Packages help in logical grouping of classes and interfaces.
Chapter 3 - Java Programming With Supplied Classes1 Chapter 3 Java Programming With Supplied Classes.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
1 Lecture 2 Java Packages  What are Java packages?  Why do wee need packages?  How to create a Java package?  Some examples.
1 Chapter 3 Programs and Packages. 2 Java Virtual Machine (JVM) Java programs execute on the JVM. The JVM is a virtual rather than a physical machine,
Unit 051 Packages What is a Package? Why use Packages? Creating a Package Naming a Package Using Package Members Managing Source and Class Files Visibility.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Java 程序设计 Java Programming Fall, Contents for Today Java Program Structure  How to Compile a Java Program  How to Run a Java Program Environment.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA ‏ Packages.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
An intro to programming. The purpose of writing a program is to solve a problem or take advantage of an opportunity Consists of multiple steps:  Understanding.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Java programming Package. A package is a group of similar types of classes, interfaces and sub-packages. Package can be categorized in two form, built-
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Chapter1 Introduction to Java Overview of the Java What Is the Java Technology The Java Virtual Machine A First Application in Java A First Applet in Java.
Parts of JAVA 1www.gowreeswar.com. Features of JAVA 2www.gowreeswar.com.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
Class Libraries Chapter 1 1 Source Intro to Java Programming Y. Daniel Liang.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
9.1 Java Packages A collection of classes Allows classes to be grouped arbitrarily Hierarchical structure independent of inheritance Classes can.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations.
Packages. The main feature of OOP is its ability to support the reuse of code: –Extending the classes –Extending interfaces The features in basic form.
CSI 3125, Preliminaries, page 1 Packages. CSI 3125, Preliminaries, page 2 Packages Packages are containers for classes Collection of classes Packages.
April 20, 1998CS102-02Lecture 4-1 A Method to the Madness CS Lecture 4-1 Java's Work Horses.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Object Oriented Programming in Java Habib Rostami Lecture 10.
Object and Classes อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 3.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Java Packages  What are Java packages?  Why do we need packages?  How to create a Java package?  Some examples.  Coming Next: Program Documentation.
1 Stack and Heap. 2 “Stack” Stack – a linear data structure in which items are added and removed in last-in, first-out order. Definition: context – the.
Java Packages - allow you to group classes as a collection Standard Java library has packages: java.lang and java.util … Also hierarchical: java and javax.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Programming With Java ICS201 University Of Ha’il1 ICS 201 Introduction to Computer Science Inheritance.
Packages In Java.
CSC 205 Programming II Lecture 5 AWT - I.
Chapter 7 User-Defined Methods.
“Packages in Java”.
Topic: Packages Course : JAVA PROGRAMMING Paper Code: ETCS-307
Packages When we name a program , we name it by class name…
Java Basics Packages.
Interfaces.
Packages Written part of final exam Alex Rudniy
Packages and Interfaces
Object Oriented Programming in java
Sampath Kumar S Assistant Professor, SECE
Constructors, GUI’s(Using Swing) and ActionListner
PACKAGES.
Interfaces,Packages and Threads
Presentation transcript:

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

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

Chapter 8 Nested Classes & Interfaces, & Exceptions 6

7

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 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

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

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

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 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 { … }

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 { … }

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(); }

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 包在同一个目录中。 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(); }