java语言程序设计 进阶篇 原书第八版 课件 PPT(第二十一章)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
8
No Casting Needed
ArrayList<Double> list = new ArrayList<Double>(); list.add(5.5); // 5.5 is automatically converted to new Double(5.5) list.add(3.0); // 3.0 is automatically converted to new Double(3.0) Double doubleObject = list.get(0); // No casting is needed double d = list.get(1); // Automatically converted to double
3
Fix the Warning
public class ShowUncheckedWarning { public static void main(String[] args) { java.util.ArrayList<String> list = new java.util.ArrayList<String>(); ` list.add("Java Programming"); } }
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
5
Why Generics?
The key benefit of generics is to enable errors to be detected at compile time rather than at runtime. A generic class or method permits you to specify allowable types of objects that the class or method may work with. If you attempt to use the class or method with an incompatible object, the compile error occurs.
nefits of generics (§21.1). To use generic classes and interfaces (§21.2). To declare generic classes and interfaces (§21.3). To understand why generic types can improve reliability and readability (§21.3). To declare and use generic methods and bounded generic types (§21.4). To use raw types for backward compatibility (§21.5). To know wildcard types and understand why they are necessary (§21.6). To convert legacy code using JDK 1.5 generics (§21.7). To understand that generic type information is erased by the compiler and all instances of a generic class share the same runtime class file (§21.8). To know certain restrictions on generic types caused by type erasure (§21.8). To design and implement generic matrix classes (§21.9).
Chapter 21 Generics
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
1
Objectives
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
9
Declaring Generic Classes and Interfaces
Improves reliability
Compile error
7
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
No compile warning on this line.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
6
Generic Type
package ng; public interface Comaprable { public int compareTo(Object o) } package ng; public interface Comaprable<T> { public int compareTo(T o) } (b) JDK 1.5
(a) Prior to JDK 1.5
Runtime error
Comparable c = new Date(); System.out.println(pareTo("red")); (a) Prior to JDK 1.5
Generic Instantiation
Comparable<Date> c = new Date(); System.out.println(pareTo("red")); (b) JDK 1.5
4
What is Generics?
Generics is the capability to parameterize types. With this capability, you can define a class or a method with generic types that can be substituted using concrete types by the compiler. For example, you may define a generic stack class that stores the elements of a generic type. From this generic class, you may create a stack object for holding strings and a stack object for holding numbers. Here, strings and numbers are concrete types that replace the generic type.
Generic ArrayList in JDK 1.5
jav a.u til.A rra y L ist
+ A rra y L ist() + a d d (o : O b ject) : v o id + a d d (in d ex : in t, o : O b ject) : v o id + c lea r(): v o id + c o n ta in s(o : O b ject): b o o lea n + g et(in d ex : in t) : O b jec t + in d ex O f(o : O b jec t) : in t + isE m p ty(): b o o lea n + la stIn d ex O f(o : O b jec t) : in t + rem o v e(o : O b jec t): b o o lea n + size(): in t + rem o v e(in d ex : in t) : b o o lea n + set(in d ex : in t, o : O b jec t) : O b jec t (a ) A rra y L ist b efo re JD K 1 .5
jav a.u til.A rra y L ist< E >
+ A rra y L ist() + a d d (o : E ) : v o id + a d d (in d ex : in t, o : E ) : v o id + c lea r(): v o id + c o n ta in s(o : O b ject): b o o lea n + g et(in d ex : in t) : E + in d ex O f(o : O b jec t) : in t + isE m p ty(): b o o lea n + la stIn d ex O f(o : O b jec t) : in t + rem o v e(o : O b jec t): b o o lea n + size(): in t + rem o v e(in d ex : in t) : b o o lea n + set(in d ex : in t, o : E ) : E (b ) A rra y L ist in JD K 1 .5
合集下载
java语言程序设计 进阶篇 原书第八版 课件 PPT(第三十二章)
(1 ) A listen er ob ject is an in stan ce of a listen er in terface
listen er: Listen erC lass
Figure 15.3
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Chapter 32 JavaBeans and Bean Events
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
NOTE: This chapter does not require that you use any builder tools. If you are interested to use JavaBeans in rapid Java application development using JBuilder or Sun ONE Studio, please refer to Supplement I, “Rapid Java Application Development Using JBuilder” or Supplement J, “Rapid Java Application Development Using Sun ONE Studio,” on the companion Website.
JavaBeans is a software component architecture that extends the power of the Java language by enabling wellformed objects to be manipulated visually at design time in a pure Java builder tool, such as JBuilder and NetBeans.
Java语言程序设计ppt课件
10
Java 是体系结构中立的(续)
Windows下 C 语言编程过程
C 源程序(扩展名.c) C编译程序
Windows可执行文件 (扩展名.exe) 执行
Windows操作系统
Java 语言编程过程
Java 源程序(扩展名.java) Java编译程序
Java字节码文件 (扩展名.class)
执行 Java虚拟机(JVM)
18
1.1.5 独立运行的Application • Applet运行时的窗口界面是由浏览器提供的,因
此它不能脱离浏览器而独立运行。而 Application 则和任何Windows应用程序一样自建窗口界面, 可以独立运行。事实上,Java语言就是一门高级 编程语言,和其他高级编程语言并无两样。 • 图1.2所示为一个图形方式的Java Application, 运行结果和普通的Windows应用程序完全一样。
• 按Java的应用环境划分 – J2EE(Java 2 Platform Enterprise Edition) – J2SE(Java 2 Platform Stand Edition)
– J2ME(Java 2 Platform Micro Edition)
22
1.2.3 Java 开发工具 • 商业软件
21
1.2.2 JDK 的版本
• JDK的版本 – JDK 1.02 (1995) – JDK 1.1 (1996) – Java 2 SDK 1.2 (1998) 此版本以后称为Java 2 – Java 2 SDK 1.3 (2000) – Java 2 SDK 1.4(我们使用的版本) – Java 2 SDK 1.5(最新)
– Borland JBuilder – Microsoft Visual J++ – Symantec Café – Forte by Sun MicroSystems – IBM Visual Age for Java • 开源软件或共享软件 – Eclipse – Jcreator – NetBean
Java 是体系结构中立的(续)
Windows下 C 语言编程过程
C 源程序(扩展名.c) C编译程序
Windows可执行文件 (扩展名.exe) 执行
Windows操作系统
Java 语言编程过程
Java 源程序(扩展名.java) Java编译程序
Java字节码文件 (扩展名.class)
执行 Java虚拟机(JVM)
18
1.1.5 独立运行的Application • Applet运行时的窗口界面是由浏览器提供的,因
此它不能脱离浏览器而独立运行。而 Application 则和任何Windows应用程序一样自建窗口界面, 可以独立运行。事实上,Java语言就是一门高级 编程语言,和其他高级编程语言并无两样。 • 图1.2所示为一个图形方式的Java Application, 运行结果和普通的Windows应用程序完全一样。
• 按Java的应用环境划分 – J2EE(Java 2 Platform Enterprise Edition) – J2SE(Java 2 Platform Stand Edition)
– J2ME(Java 2 Platform Micro Edition)
22
1.2.3 Java 开发工具 • 商业软件
21
1.2.2 JDK 的版本
• JDK的版本 – JDK 1.02 (1995) – JDK 1.1 (1996) – Java 2 SDK 1.2 (1998) 此版本以后称为Java 2 – Java 2 SDK 1.3 (2000) – Java 2 SDK 1.4(我们使用的版本) – Java 2 SDK 1.5(最新)
– Borland JBuilder – Microsoft Visual J++ – Symantec Café – Forte by Sun MicroSystems – IBM Visual Age for Java • 开源软件或共享软件 – Eclipse – Jcreator – NetBean
java语言程序设计 进阶篇 原书第八版 课件 PPT(第二十七章)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
11
Representing Edges: Adjacency Matrix
1
Objectives
To model real-world problems using graphs and explain the Seven Bridges of Kö nigsberg problem (§27.1). To describe the graph terminologies: vertices, edges, simple graphs, weighted/unweighted graphs, and directed/undirected graphs (§27.2). To represent vertices and edges using lists, adjacent matrices, and adjacent lists (§27.3). To model graphs using the Graph interface, the AbstractGraph class, and the UnweightedGraph class (§27.4). To represent the traversal of a graph using the AbstractGraph.Tree class (§27.5). To design and implement depth-first search (§27.6). To design and implement breadth-first search (§27.7). To solve the nine-tail problem using breadth-first search (§27.8). To solve the Knight’s Tour problem by reducing it to a Hamiltonian path problem (§27.9).
Java程序设计ppt课件(完整版)
是一行写不下一条语句时,允许一条语句占用多行。 • 逗号(,):分隔变量声明中的多个标识符。 • 圆括号:一般用在表达式、方法的参数和控制语句的条件表达
式中。注意圆括号可以嵌套,但需要严格配对使用。 • 方括号([]):用于声明数组,引用数组的元素值。 • 花括号({}):用于定义一个语句块,一个语句块是零条或多
1.2.4 知识总结
❖ 6.Java数据类型
▪ (1)基本数据类型
• 整数类型:byte,short,int,long。 • 浮点类型:float,double。 • 字符类型:char。 • 布尔类型:boolean。
▪ (2)引用数据类型
• 类类型:class,String,Double等。 • 接口类型:Interface • 数组类型:基本数据类型数组,对象型数组。
❖ 当主菜单与子菜单的连接成功之后, 若要保证菜单的重 复使用,则需要在主菜单及子菜单中添加循环控制语句来 实现,Java语言中的循环有while循环,do while循 环,for循环等。
1.3.3 解决方案
▪ 1、打开Eclipse。
▪ 2、添加一个包,名为com.esms,并复制Menus类到包中。
目录
第一章 Java与程序逻辑 第二章 面向对象程序设计基础 第三章 面向对象基本特性 第四章 常用对象使用 第五章 异常处理 第六章 I/O操作 第七章 多线程编程 第八章 Java的GUI可视界面编程
目录
第一章 Java与程序逻辑 第二章 面向对象程序设计基础 第三章 面向对象基本特性 第四章 常用对象使用 第五章 异常处理 第六章 I/O操作 第七章 多线程编程 第八章 Java的GUI可视界面编程
❖ 3.Java的开发环境
▪ 对于开发人员来说,除了需要上述的运行环境以外, 还需要开发环境的支持,Java的开发环境主要由以下 两部分组成。
式中。注意圆括号可以嵌套,但需要严格配对使用。 • 方括号([]):用于声明数组,引用数组的元素值。 • 花括号({}):用于定义一个语句块,一个语句块是零条或多
1.2.4 知识总结
❖ 6.Java数据类型
▪ (1)基本数据类型
• 整数类型:byte,short,int,long。 • 浮点类型:float,double。 • 字符类型:char。 • 布尔类型:boolean。
▪ (2)引用数据类型
• 类类型:class,String,Double等。 • 接口类型:Interface • 数组类型:基本数据类型数组,对象型数组。
❖ 当主菜单与子菜单的连接成功之后, 若要保证菜单的重 复使用,则需要在主菜单及子菜单中添加循环控制语句来 实现,Java语言中的循环有while循环,do while循 环,for循环等。
1.3.3 解决方案
▪ 1、打开Eclipse。
▪ 2、添加一个包,名为com.esms,并复制Menus类到包中。
目录
第一章 Java与程序逻辑 第二章 面向对象程序设计基础 第三章 面向对象基本特性 第四章 常用对象使用 第五章 异常处理 第六章 I/O操作 第七章 多线程编程 第八章 Java的GUI可视界面编程
目录
第一章 Java与程序逻辑 第二章 面向对象程序设计基础 第三章 面向对象基本特性 第四章 常用对象使用 第五章 异常处理 第六章 I/O操作 第七章 多线程编程 第八章 Java的GUI可视界面编程
❖ 3.Java的开发环境
▪ 对于开发人员来说,除了需要上述的运行环境以外, 还需要开发环境的支持,Java的开发环境主要由以下 两部分组成。
java语言程序设计 进阶篇 原书第八版 课件 PPT(第四十三章)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
8
Passing Parameters, cont.
Remote object type. Remote objects are passed differently from the local objects. When a client invokes a remote method with a parameter of some remote object type, the stub of the remote object is passed. The server receives the stub and manipulates the parameter through the stub.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
4
The Differences between RMI and Traditional Client/Server Approach
Chapter 43 Remote Method Invocation
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
8
Passing Parameters, cont.
Remote object type. Remote objects are passed differently from the local objects. When a client invokes a remote method with a parameter of some remote object type, the stub of the remote object is passed. The server receives the stub and manipulates the parameter through the stub.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
4
The Differences between RMI and Traditional Client/Server Approach
Chapter 43 Remote Method Invocation
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
Java语言程序设计(Java语言概述)ppt.ppt
12
Java的发展
五年回顾(1996-2000)
1999年
1. Jan 13, Major consumer electronics manufacturers support Java technology for digital television
2. Feb 24, Java 2 platform source code released 3. Mar 4, XML support for Java platform unveiled 4. Mar 27, Java HotSpot performance engine unveiled 5. June 2, JavaServer Pages technology unveiled 6. June 15, JavaOne developer conference draws 20,000 7. June 15, Sun announces three editions of Java platform:
Java语言程序设计
吕凤翥 马 皓
1
课程提纲
1. Java语言概述 2. Java语言语法基础 3. 面向对象的特征 4. Applet及其应用 5. 图形用户界面设计 6. 异常 7. 线程 8. 集合操作 9. Java输入/输出 10. 网络编程
2
参考资料
Java语言程序设计,吕凤翥、马皓编著,清华大 学出版社
6
Java的发展
历史记录 - Who
James Gosling: Green Team original, FirstPerson employee, original member Java Products Group, lead engineer and key architect of Java technology
Java的发展
五年回顾(1996-2000)
1999年
1. Jan 13, Major consumer electronics manufacturers support Java technology for digital television
2. Feb 24, Java 2 platform source code released 3. Mar 4, XML support for Java platform unveiled 4. Mar 27, Java HotSpot performance engine unveiled 5. June 2, JavaServer Pages technology unveiled 6. June 15, JavaOne developer conference draws 20,000 7. June 15, Sun announces three editions of Java platform:
Java语言程序设计
吕凤翥 马 皓
1
课程提纲
1. Java语言概述 2. Java语言语法基础 3. 面向对象的特征 4. Applet及其应用 5. 图形用户界面设计 6. 异常 7. 线程 8. 集合操作 9. Java输入/输出 10. 网络编程
2
参考资料
Java语言程序设计,吕凤翥、马皓编著,清华大 学出版社
6
Java的发展
历史记录 - Who
James Gosling: Green Team original, FirstPerson employee, original member Java Products Group, lead engineer and key architect of Java technology
java语言程序设计-基础篇--原书第八版--课件-PPT(第十二章)
Label
Text Check Radio
field Box
Button
Button
// Create a text field with text "Type Name Here" JTextField jtfName = new JTextField("Type Name Here");
// Create a check box with text bold JCheckBox jchkBold = new JCheckBox("Bold");
Applet
JApplet
Window
Frame Dialog
JFrame JDialog
JComponent
Swing Components in the javax.swing package
Lightweight
6
Container Classes
Object
Dimension Font
LayoutManager 1
JComponent
JPanel
Swing Components in the javax.swing package
Lightweight
8
JComponent
Swing GUI Components
JC heckBo xM e nuIte m
AbstractButton
JM enuIte m JButton JToggleButton
// Create a radio button with text red JRadioButton jrbRed = new JRadioButton("Red");
Text Check Radio
field Box
Button
Button
// Create a text field with text "Type Name Here" JTextField jtfName = new JTextField("Type Name Here");
// Create a check box with text bold JCheckBox jchkBold = new JCheckBox("Bold");
Applet
JApplet
Window
Frame Dialog
JFrame JDialog
JComponent
Swing Components in the javax.swing package
Lightweight
6
Container Classes
Object
Dimension Font
LayoutManager 1
JComponent
JPanel
Swing Components in the javax.swing package
Lightweight
8
JComponent
Swing GUI Components
JC heckBo xM e nuIte m
AbstractButton
JM enuIte m JButton JToggleButton
// Create a radio button with text red JRadioButton jrbRed = new JRadioButton("Red");
java 课件 ppt
04
Java 常用类库
String 类和 StringBuffer 类
字符串处理类
•·
String 类: Java 中的基本 数据类型,用于表示字符串 。它提供了多种方法来操作 字符串,如连接、查找、替 换等。
StringBuffer 类: 用于处理 可变字符串。与 String 类相 比,StringBuffer 提供了更 高效的方法来修改字符串, 因为它在内存中直接修改字 符数组,而不是创建新的 String 对象。
07
Java 多线程编程
线程的创建和管理
继承Thread类
通过继承Thread类并重写run()方法, 可以创建新的线程类。
线程的启动和终止
使用Thread类的start()方法启动线程 ,使用interrupt()方法中断线程。
实现Runnable接口
通过实现Runnable接口并重写run() 方法,可以创建新的线程类。
IO 流和文件操作
01
数据输入输出类
02
•·
03
InputStream 类和 OutputStream 类: 用于读取和写入字节流。 InputStream 用于读取数据,OutputStream 用于写入数据。
04
FileReader 类和 FileWriter 类: 用于读取和写入字符流。 FileReader 用于读取文本文件,FileWriter 用于写入文本文件。
运算符和控制流
for循环
重复执行一段代码指定的次数。
while循环
只要条件为真,就重复执行一段代码 。
面向对象编程基础
类和对象 类是对象的模板,定义了对象的属性和方法。 对象是类的实例,具有类定义的属性和方法。
java语言程序设计 基础篇 原书第八版 课件 PPT(第十三章)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807
2
Objectives
Show runtime error
Quotient Run
Fix it using an if statement
QuotientWithIf Run
What if the runtime error occurs in a called method?
QuotientWithException Run
System errors are thrown by JVM and represented in the Error class. The Error class describes internal system errors. Such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully.
Now you see the advantages of using exception handling. It enables a method to throw an exception to its caller. Without this capability, a method must handle the exception or terminate the program.
Java程序设计(高校系列教材课件).ppt
1.21
高等学校计算机应用人才培养系列教材
常量、关键字和标识符
常量
常量代表某一特定类型的具体值,常量有数字常量、 布尔常量、字符常量和字符串常量之分。
数字常量包括两种:整型常量和浮点型常量。常用的 整型常量都是十进制的。整型常量还有另外两种进制 ,八进制和十六进制。在Java中,八进制的值通过在 它的前面加一个前导0来表示,而通过前导的0x或0X来 表示一个十六进制的值。
) 分布式(Distributed)
高等学校计算机应用人才培养系列教材
1.7
Java和Internet
在使用Java时,用户将从Internet下载Java字节码并在自 己的计算机上运行。在Web网页中运行的Java程序叫做 Applet。要使用Applet,需要有支持Java的浏览器,它可 以解释字节码。
True或false
1.19
高等学校计算机应用人才培养系列教材
Java的数据类型和变量
变量
变量是Java的一个基本存储单元。在Java中,在使用变量之前需要先 声明变量。变量声明通常包括三部分:变量类型、变量名、初始值, 其中变量的初始值是可选的。
以下是几个各种变量声明的例子,有一些包括了变量的初始化。
char
boolean
所占用字节数 1 2 4 8 4 8
2
1
范围 -128到127 -32,768到32, 767 -2,147,483,648到2,147,483,647 -9,223,372,036,854,775,808到 -9,223,372,036,854,775,807 大约 ±3.4+38 大约 ±1.7E+308
1.10
高等学校计算机应用人才培养系列教材
