欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    ch4_Packages_包ppt课件.pptx

    • 资源ID:16977153       资源大小:1.24MB        全文页数:24页
    • 资源格式: PPTX        下载积分:20金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要20金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    ch4_Packages_包ppt课件.pptx

    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), interfaces 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 也采用分层的文分层的文件系统来管理源文件和字节码文件件系统来管理源文件和字节码文件。每个包都有名字,包包含彼此有关系的类(例如基类与其继承类)接口和/或其它包。同名的类不能放到同一个包里,但可以放到不同的包。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 (/), (), 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, that 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 structure 类库与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, which 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 classes.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.applet 包含与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. 压缩文件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, youll 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 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 mypack.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.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(String 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.util /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 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 packages. 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 affect the performance and size of the class. 如果需要用java.long包以外的类包以外的类,必须利用必须利用import关键字关键字准确地告诉编译器所需的类。import语句通知编译器将指定包里的一个或所有类导入当前文件。然而缺省缺省包没有名字无法包没有名字无法导入导入,所以要创建包。Import语句必须在语句必须在package语句之后,其它语句之前。语句之后,其它语句之前。 import keyword example import ; /import java.util.*; public class HelloDate public static 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;public 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, but 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 them 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 Java 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 method 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 automatically 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类是所有类的根类类是所有类的根类。它有很多常用的方法,像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 (String 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 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和对象的哈希地址。有时希望输出特定希望输出特定的的内内容容,例如点的坐标而不是对象的地址,这时,可以重写重写 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 package 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。

    注意事项

    本文(ch4_Packages_包ppt课件.pptx)为本站会员(春哥&#****71;)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开