ch4_Packages_包ppt课件.pptx





《ch4_Packages_包ppt课件.pptx》由会员分享,可在线阅读,更多相关《ch4_Packages_包ppt课件.pptx(24页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、ch4_Packages_包Chapter 4 Packages 包4.1 Concept of packages 包的概念 A package corresponds to a folder in the file management system. Java uses a hierarchical file system to manage source and .class files too. A package is a collection of related classes (e.g. base classes and there sub classes), interfac
2、es and/or other packages. Each package has its own name. Classes and interfaces with same names cannot appear in the same package, they can appear in different packages. 包包与文件管理系统中的文件夹对应与文件管理系统中的文件夹对应,Java 也采用分层的文分层的文件系统来管理源文件和字节码文件件系统来管理源文件和字节码文件。每个包都有名字,包包含彼此有关系的类(例如基类与其继承类)接口和/或其它包。同名的类不能放到同一个包里,
3、但可以放到不同的包。A package contains a group of classes. Packages in Java Java use a hierarchical (层次) file system to manage source and .class files. Dots are used to separate package names. For example, in javax.swing, a dot separates package name javax from sub package name swing. A dot is equivalent of (
4、/), (), to separate directory names. Sub package1. Packages allow you to organize related classes and interfaces into smaller units and make it easy to locate and to use.2. The Package mechanism is name space management, can avoid naming conflicts.3. Package names can be used to identify classes, th
5、at is, a full class name includes a package name, like iava.util.Date.4. By convention, companies use their reversed Internet domain name in their package names, like: .包利于将类和接口组织成较小的单位,方便使用。包是一种命名空间管理机制,能够防止名字冲突。包名可以作为类名的一部分。事实上,类名的全称包含包名,例如。通常,一些公司用将互联网的域名做包名。4.2 Java library and its package struc
6、ture 类库与Java类库的包组织结构 Java JRE provides a powerful class library for use. All built-in classes in the library are organized into several packages. The structure of Java packages as shown in figure below. Java JRE 提供了强大的类库。类库里的类分别放在不同的包里,包结构包结构如下图所示:1. java.lang includes language related classes, whic
7、h is imported into a source program automatically. 2. java.io includes input/output related classes, like file dealing classes.3. java.util includes useful classes, like Date, Scanner classes.4. includes network communication related classes.5. java.swing includes GUI, Windows application related cl
8、asses.6. java.applet includes small web application related classes. 1. java.lang 包含包含与语言相关的类与语言相关的类。该包由解释程序自动导入该包由解释程序自动导入Java源程序。源程序。2. java.io 包含与输入/输出相关的类,例如与文件相关的类,用于对不同设备输入输出数据的处理。3. java.util 包含实用工具类实用工具类,如Date,Scanner等。4. 包含与网络通信网络通信相关的类。5. java.swing 包含与开发GUI,窗口应用程序,窗口应用程序相关的类。6. java.apple
9、t 包含与Java小程序小程序,web应用程序相关的类。rt.jar You can find all classes in Java library. At Javajdk1.7.0jrelib, youll see a compressed jar file. The rt.jar is an executable jar file. Under Java package (folder), all sub packages there. Opening a sub package, youll see all classes bytecode, .class files in it. 压
10、缩文件rt.jar是可执行的是可执行的jar文件文件。在它的java包里,你能看到全部子包。打开子包,可以看到子包里所有类的字节码,.class 文件。rt.jar4.3 Create packages 创建包 As a rule, putting each java source file into a package, you need to create packages to store your java source files and bytecode files. In eclipse, select a java project, then new-package, youl
11、l enter into the “Create java package” dialogue, at the package name textbox, type in a package name. Dots are used to separate package names. E.g. sub1.sub2, the dot separates package sub1 from sub package sub2. A dot is equivalent of “/”, “”, to separate a folder from its sub folder. In fact, each
12、 package produces two sub folders which take the package name under src and bin or their sub folders respectively, src for .java and bin for .class files respectively. Creating java classes in a package, the package will be putted into a source file as the first statement automatically.package mypac
13、k.sub1.sub2;/first linepublic class HelloWorld public static void main(String args) System.out.println(Hello World); package org;public class Calculator public int add(int x, int y) return( x + y ) ; 4.3 Create packages 创建包 在eclipse环境下,选中一个Java项目,通过New,Package进入创建Java包对话框,在包名称文本框里输入包名。用“.”分隔子包名。例如p2
14、.p3,“.”将包p2与子包p3隔开。“.”与文件夹名里的“/”、“”的作用一样。事实上,创建一个Java包会分别在src和bin文件夹或者它们的子文件夹下生成与包名相同的子文件夹,src 下是.java, bin下是 .class文件。在一个包里创建Java类时,包语句会被自动地加入包语句会被自动地加入文件,成为第一条语句第一条语句。4.3 Create packages 创建包/ only comment can be herepackage org.p1.p2; /first linepublic class HelloWorld public static void main(Stri
15、ng args) System.out.println(Hello World); package org;public class Calculator public int add(int x, int y) return( x + y ) ; There are two ways in order to use the public classes stored ina package. 1. Declare the full class name (includes package name). E.g. use class Date.class in package java.uti
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ch4_Packages_ ppt 课件

限制150内