Download presentation
Presentation is loading. Please wait.
1
Package & Java Access Specifiers
Chapter 5 Hiding the Implementation Package & Java Access Specifiers
2
// Graphics.java package graphics; public abstract class Graphic { … } // Circle.java public class Circle extends Graphic implements Draggable {
3
// Rectangle.java package graphics; public class Rectangle extends Graphic implements Draggable { … } // Draggable.java public interface Draggable {
4
Creating and Using Packages
// Graphics.java package graphics; public abstract class Graphic { … } // Circle.java public class Circle extends Graphic implements Draggable { // Rectangle.java public class Rectangle extends Graphic implements Draggable { // Draggable.java public interface Draggable { Creating and Using Packages
5
You should bundle these classes and the interface in a package for several reasons:
1、You and other programmers can easily determine that these classes and interfaces are related. 2、You and other programmers know where to find classes and interfaces that provide graphics-related functions.
6
3、The names of your classes won’t conflict with class names in other packages, because the package creates a new namespace. 4、You can allow classes within the package to have unrestricted access to one another yet still restrict access for classes outside the package.
7
Definition: A package is a collection of related classes and interfaces providing access protection and namespace management.
9
Naming a Package The fully qualified name:
graphics包: Rectangle 和 java.awt包 :Rectangle 不冲突 The fully qualified name: graphics.Rectangle 和 java.awt.Rectangle they are in different packages, and the fully qualified name of each class includes the package name. That is, the fully qualified name of the Rectangle class in the graphics package is graphics.Rectangle, and the fully qualified name of the Rectangle class in the java.awt package is java.awt.Rectangle.
10
com.company.region.package. cn.edu.jmu.timer.Time
Naming a Package By Convention: Companies use their reversed Internet domain name in their package names, for example, com.company.region.package. cn.edu.jmu.timer.Time
11
Using Package Members Refer to the member by its long (qualified) name
Only public package members are accessible outside the package in which they are defined. To use a public package member from outside its package, you must do one or more of the following: Refer to the member by its long (qualified) name Import the package member Import the members entire package import cn.edu.jmu.javaprogs.Time1; import cn.edu.jmu.javaprogs.*; import java.awt.*; import java.awt.event.*;
13
Managing Source and Class Files
class name graphics.Rectangle pathname to file graphics/Rectangle.java
15
You could arrange your source and class directories separately , as shown below:
16
Classpath参数 Definition: A class path is an ordered list of directories or ZIP files in which to search for class files
17
set classpath = D:\javaSample\class;
18
Time.java: package cn.edu.jmu.timer; 将Time类放入包cn.edu.jmu.timer中, 源文件Time.java存放在与包相对应的目录结构下。 TestTime.java: import cn.edu.jmu.timer.Time; 导入Time类。
19
// Time.java package cn.edu.jmu.timer; import java.text.DecimalFormat; //用于数据格式化 // 24小时制时间 public class Time extends Object { private int hour; // 0 ~ 23 private int minute; // 0 ~ 59 private int second; // 0 ~ 59 // Time类构造器,初始化每个实例对象的值为0 // 确保每个Time对象处于一致的状态 public Time() { setTime( 0, 0, 0 ); } // 数据的正确性验证,对无效数据取0值 public void setTime( int h, int m, int s ) hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); second = ( ( s >= 0 && s < 60 ) ? s : 0 );
20
//将通用时间格式转换为字符串 public String toUniversalString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return twoDigits.format( hour ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ); } //将标准时间格式转换为字符串 public String toString() return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ) + ( hour < 12 ? " AM" : " PM" );
21
// TestTime.java // TestTime类需要导入Time类 import javax.swing.JOptionPane; import cn.edu.jmu.timer.Time; // import语句导入Time类 public class TestTime { public static void main( String args[] ) { Time t = new Time(); t.setTime( 13, 27, 06 ); String output = "Universal time is: " + t.toUniversalString() + "\nStandard time is: " + t.toString(); JOptionPane.showMessageDialog( null, output, "Packaging Class Time1 for Reuse", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); }
23
Attention: Reading Thinking in Java chapter 5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.