《ch4_Packages_包ppt课件.pptx》由会员分享,可在线阅读,更多相关《ch4_Packages_包ppt课件.pptx(24页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
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
16、l /Use package name java.util.Date x=new java.util.Date();4.4 Import packages 导入包package nameclass namefull class namepublic class HelloDate public static void main(String args) System.out.println(Hello, its: ); System.out.println(new java.util.Date(); 2. Use import keyword If you need to use other
17、classes not included in package java.long, you must tell Java compiler exactly what classes you want by using the import keyword. An import tells the compiler to bring in a class or all classes in a specific package. However, the default package cant be imported for lack of package name, so create p
18、ackages. The import statements must be in the front of other statements and behind the package statement. Import a specific package member: e.g. import mypackage.Calculator; or import all classes in a package: e.g. import mypackage.*; Using *, the file will be compiled more slowly. But * does not af
19、fect the performance and size of the class. 如果需要用java.long包以外的类包以外的类,必须利用必须利用import关键字关键字准确地告诉编译器所需的类。import语句通知编译器将指定包里的一个或所有类导入当前文件。然而缺省缺省包没有名字无法包没有名字无法导入导入,所以要创建包。Import语句必须在语句必须在package语句之后,其它语句之前。语句之后,其它语句之前。 import keyword example import ; /import java.util.*; public class HelloDate public sta
20、tic void main(String args) System.out.println(Hello, its: ); System.out.println(new Date(); package org.p1.p2;import world.moon.*; public class For public static void main(String args) HelloMoon hm=new HelloMoon( rabbit hole ); My m=new My(); System.out.println(hm.getHoleName(); package world.moon;p
21、ublic class HelloMoon private String holeName; public String getHoleName() return holeName; public HelloMoon(String hName) holeName = hName; Example of packagespackage world.moon; public class My public My() System.out.println(MyPackage!); We can use classes in a package by importing the package, bu
22、t cant use any classes that belong to its sub package. For example, classes Scanner, Date are in package java.util, util is the sub package of java, you can only import java.util.*, instead of import java.*. 不能通过不能通过import导导入包,然后使用它的入包,然后使用它的子包里的类子包里的类。 If packages contain same classes, identify the
23、m by package names. 如果多个包包含相同的类,通过包名区分它们。For examples: import tom.jiafei.*; /contains class AA import .*; /contains class AA tom.jiafei.AA a=new tom.jiafei.AA(); .AA ();Note 注意注意4.5 Package java.lang Package java.lang contains all language related classes. java.lang is imported implicitly in every J
24、ava code file by java explainer, so never use the import keyword to import java.lang to a java files. java.lang包含了与Java语言相关的类。包里的部分类如下图所示。Java解释器解释器自动自动地地将将java.lang包加入每个包加入每个java源文件源文件,因此,不要不要通过import将java.lang导入你的java文件。java.lang4.6 Useful classes in package java.lang4.6.1 Object and toString meth
25、od Package java.lang contains a collection of language related classes organized in the java packages lang sub package. Object is the root class of all. It has several common used methods, some of them we already used, like String, System, especially the toString(). Method toString() is automaticall
26、y called in special situations when the compiler wants a String but it has an object. The prototype of toString(): public String toString() Its return type is String. When a String is wanted, but an object there, toString() returns: class name+ +Hash address of the object. Object类是所有类的根类类是所有类的根类。它有很
27、多常用的方法,像String,System,特别是toString()方法。当需要String类型,而实际类型是对象时,toString()会被自动地调用。toString() 的原型的原型是: public String toString() 它的返回类型是返回类型是String。当需要String,而实际类型是对象时,toString()返回:类名+对象的Hash码地址。toString()public class Point private int x,y; public Point(int a, int b) x=a; y=b; public static void main (Str
28、ing args) Point x=new Point(4,3); System.out.println(x); /or System.out.println(x.toString(); Output:Point119dc16 返回对象的类名,后跟Hash码。 Where, method println() needs a String parameter, but the actual parameter x is an object, so toString() is invoked automatically, and outputs class name Point and Hash
29、address of x. At times, we dont care about an objects address, but special things, like coordinates of a point, in that case, just override (different from overloading) toString().toString() example 其中,println需要String参数,而实际是对实际是对象象x,所以toString()就被自动地调用,输出类名Point和对象的哈希地址。有时希望输出特定希望输出特定的的内内容容,例如点的坐标而不
30、是对象的地址,这时,可以重写重写 toString()。public class Point private int x,y; public Point(int a, int b) x=a; y=b; public String toString() return point:+x+,+y; public static void main (String args) Point x=new Point(4,3); System.out.println(x); Output:point:4,3toString()4.6.2 System Class System in java.lang pac
31、kage has several fields, one of is out. The out is static, of type PrintStream. Since its static, you dont need to create anything with new. The out object is always there for you. PrintStream has a list of methods you can call. For now all were interested in are print( )s and println()s e.g. (“Hello World!”); System的fields中有一个是out,它是static的,类型为PrintStream。由于是静态的,不需要用new创建对象就可以使用,PrintStream类有很多方法,目前用得最多的是print和println。