1 / 123

SSD1: Introduction to Information Systems

SSD1: Introduction to Information Systems. Unit 2: Introduction to Java and Object-Oriented Programming. Shen Zhidong shenzd@gmail.com ISS, Wuhan University. 提 纲. 程序设计语言概述 JAVA 简介 Programming with Java. 程序设计语言. 软件的基本组成部分是完成其功能的程序。 程序是指按照时间顺序依次安排的工作步骤。而程序设计则是对这些步骤的编排和优化。

Download Presentation

SSD1: Introduction to Information Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SSD1: Introduction to Information Systems Unit 2: Introduction to Java and Object-Oriented Programming Shen Zhidongshenzd@gmail.comISS, Wuhan University

  2. 提 纲 • 程序设计语言概述 • JAVA简介 • Programming with Java

  3. 程序设计语言 软件的基本组成部分是完成其功能的程序。 程序是指按照时间顺序依次安排的工作步骤。而程序设计则是对这些步骤的编排和优化。 程序设计有着比计算机更长的历史,只不过计算机的出现使得程序设计有了更专用的领域——计算机程序设计,并得到空前的发展。 计算机程序设计又称为编程(programming),是一门设计和编写计算机程序的科学和艺术。

  4. 程序设计语言 程序设计语言(也被称为“编程语言”,Programming Language)是人们编制程序所使用的语言。 程序描述了计算机处理数据、解决问题的过程,这是程序的实质。 但程序的描述形式却可以是多种多样的,可以用不同的方式表述——不同的程序设计语言。

  5. 程序与计算机 现代计算机都是基于冯·诺伊曼模型结构的,此模型着眼于计算机的内部结构,定义了处理机的运行过程。该模型把计算机分为四个子系统: • 存储器 • 算术/逻辑单元 • 控制单元 • 输入/输出单元 冯·诺伊曼模型中,程序是由一组数量有限的指令组成,程序存储在内存中,程序依据算法完成运算任务。

  6. 程序设计语言 • 程序设计语言的种类: • 机器语言:机器指令,直接执行 • 汇编语言:符号化,汇编器(Assembler) • 高级程序设计语言(编译型) • 高级程序设计语言(解释型)

  7. 程序设计语言 ——高级程序设计语言 程序中基本的动作单位被称为“语句”,不同的语句实现不同的功能。 用高级语言书写的程序不可能直接地在计算机上执行。

  8. 程序设计语言 ——高级程序设计语言 • 要在计算机上执行高级语言书写的程序,有两种基本方法: • 编译(Compilation)执行:这种方法是设法预先把高级语言程序(也称为“源程序”)转换成为机器语言的可以由计算机直接执行的程序,即转变为“可执行(Executable)程序” 。如:C++。 • “解释”(Interpretation)执行:这种方法是即时的把源程序转换为机器可执行的指令。有时为了提高效率,也可以先编译成一种中间代码。如:Java

  9. 程序设计语言 ——高级程序设计语言 • 对于“编译执行”方式,人们实现了高级语言“编译器”(Compiler)完成这种转换工作。编译器把高级语言程序看成是符合一定语法结构的符号串,对它进行加工变换。 • 编译器对源程序的加工一般分为两个阶段: • 首先,源程序被翻译成机器语言,这种翻译结果称为“目标码”(object code),目标码构成的程序片段称为目标模块。 • 第二步,这些目标模块被与其他一些基本模块(由编译软件提供)连接在一起,最终形成“可执行程序”(executable program),这样的程序就可以在计算机上实际运行了。 • 第一个加工步骤称为“编译”(compiling),第二个步骤称为“连接”(linking)。

  10. 程序设计语言 ——高级程序设计语言 编 译 过 程

  11. 程序设计语言 ——高级程序设计语言 对于“解释执行” 方式,人们实现了一种称为“解释器”(Interpreter)的软件来完成转换工作。解释器在工作方式上与编译器不同,它不对源程序进行翻译,而是直接对源程序的语句进行分析和解释,实现源程序所描述的功能。 解 释 过 程

  12. 程序设计语言 ——高级程序设计语言 高级语言写出的程序是独立于任何具体的计算机的。但是“编译器”和“解释器”则是与机器相关的。同一种语言,对于不同的机器,需要不同的“编译器”和“解释器”。 人们已经开发出了许多不同的高级程序设计语言,不同的语言有各自不同的规范,因此需要专门的“编译器”和“解释器”。

  13. 程序设计语言 ——高级程序设计语言 • FORTRAN(FORmular TRANslation)语言,HPF:IBM,1950 • Pascal语言:N.Wirth,1968,(Delphi,Borland) • COBOL(COmmon Business Oriented Language)语言: 美国国防部,1960 • C和C++语言:Dennis Retchie, 1972; Bjarne Stroustrup,1983, Bell • Basic(Beginner's All-purpose Symbolic Instruction Code)语言:美国国防部,1964 • Ada语言:军用程序设计语言, 美国国防部, 1983 • Java语言: 1991年,SUN MicroSystem公司

  14. JAVA简介 ---Java历史及发展 • Java是一种解释型的、面向对象的编程语言。 • 历史:1991年,SUN MicroSystem公司的Jame Gosling、Bill Joe等人为在电视、控制烤箱等家用消费类电子产品上进行交互式操作而开发了一个名为Oak的软件。 • Oak —> Java:互联网给Java带来新的生机 • 1995年Sun向公众推出Java并引起业界的轰动 • 发展:面向网络应用,类库不断丰富,性能不断提高,应用领域不断拓展。(1995年以来) • 应用:Java是当今最流行的软件开发语言之一,Sun推出了 Java2 & J2EE使Java具有更广泛的用途,适用于各种应用开发,尤其是网络应用,网络服务,嵌入式系统等。

  15. James Gosling: Green Team original, FirstPerson employee, original member Java Products Group, lead engineer and key architect of Java technology Bill Joy: Cofounder and vice president, Sun Microsystems, Inc., principal designer of the University of California, Berkeley, version of the UNIX operating system Eric Schmidt: Former Sun Microsystems chief technical officer, Former Novell chairman and CEO, Chairman of the Board and CEO, Google Inc. Java的发展 • 历史记录 - Who

  16. Java的发展 • http://java.sun.com/

  17. 什么是Java? • Java编程语言的特点 • 简单(Simple) • 面向对象(Object oriented) • 解释性(Interpreted) • 平台独立和可移植(Architecture neutral & Portable) • 鲁棒和安全(Robust & Secure) • 多线程(Multithreaded) • 分布式(Distributed) • 动态(Dynamic)

  18. 什么是Java? • Java可以做什么? • 基本: Objects, strings, threads, numbers, input and output, data structures, system properties, date and time等 。 • Applets应用,嵌入HTML文档。 • 网络应用:实现基于URL/TCP/UDP的网络应用。 • 安全应用: 电子签名、公钥和私钥管理、访问控制和用户证书等。 • 国际化应用: 实现程序的本地化,并以适当的语言显示 • 软件构件: JavaBeans。 • 对象串行化: 实现远程方法执行(Remote Method Invocation, RMI)。 • Java数据库连接(Java Database Connectivity, JDBC): 提供统一的方式访问关系型数据库。

  19. 什么是Java? • 为什么选择Java? • 简单易学(Get started quickly) • 写较少的代码 (Write less code) • 写更好的代码 (Write better code): garbage collection (防止内存泄漏),面向对象、JavaBeans构件机制、其他API复用代码,减少bugs • 开发程序更快 (Develop programs more quickly) • 避免平台依赖性(Avoid platform dependencies with 100% Pure Java) • 编写一次,随处运行(Write once, run anywhere) machine-independent bytecodes • 发布软件更容易 (Distribute software more easily): 指对软件的更新,利用动态加载功能,一处改动,自动更新,不用重新编译整个程序

  20. JAVA程序 • 使用Java可以开发从命令行应用程序到图形用户界面应用程序、从桌面应用程序到Web应用程序、从小型嵌入式系统到大型分布式企业级应用等多种多样的程序。 • 通常所指的Java程序可以分为: • 命令行应用程序(Command-line Application) • 图形界面应用程序(GUI Application) • 小程序(applet) • 服务端小程序(servlet) • 服务器页面(JavaServer Pages, 检查JSP) • Web应用程序 • 嵌入式应用程序 • 企业级应用程序

  21. JAVA的运行环境 • Java程序运行在JAVA平台上,Java平台可以运行于Windows、 Linux、Solaris等操作系统上。 • Java平台由Java虚拟机(JVM)和Java编程接口(API)组成。 • Java虚拟机屏蔽了不同操作系统的差异 • Java API 为程序员提供了统一的编程接口 • Java API和JVM将Java程序从对硬件的依赖中分离出来,从而实现了Java程序对操作系统和硬件平台的无关性。

  22. JAVA的运行环境 Java程序 Java API Java平台 Java虚拟机 操作系统 硬件平台

  23. 字节码文件(.class) Java解释器 即时编译器 运行系统 操作系统 JAVA平台 Java虚拟机 Java虚拟机实际上也是运行在操作系统上的一种程序,它可以解读Java字节码,并执行Java字节码。

  24. JAVA平台 • Java API是软件组件的集合,提供很多有用的功能。 • 根据应用领域的不同,API分为三大类 • Java Core API:由Sun公司指定的基本的API,任何Java平台都必须提供。 • Java Standard Extension API (javax):由Sun公司指定的扩充API,Java平台可以选择性地提供或加装 • 厂商或组织提供的API:由各家公司或组织提供。

  25. JDK开发工具包 • 提供Java开发工具包Java Development Kit(JDK)的目的是为程序开发者提供编写、测试、执行程序的一套完备的工具体系。 • 总体来说,JDK由七部分组成 • Javac:编译器 • Java:解释器 • Appletviewer:Applet显示器 • Jdb:调试器(Debugger) • Javap:分解器 • Javadoc:文档生成器 • Javah:C语言头文件生成器

  26. 搭建基本的JAVA开发环境 • 构建基本的Java开发环境是开发Java程序的前提和基础。 • JDK是Java开发工具的简称(Java Development Kit),已经推出了1.3,1.4版本,1.5版本,1.6版本。 • JavaSDK是开发Java程序的基础 • 目前,Java有3个版本 • Java ME(Java Platform Micro Edition):适用于小型设备和智能卡 • Java SE (Java Platform Standard Edition):适用于桌面系统的Java平台标准版 • Java EE(Java Platform Enterprise Edition):适用于创建服务器应用程序,开发企业应用程序的Java平台企业版

  27. JAVA程序的运行机制 • Java既可以被编译,又可以被解释。 • Java的源代码(后缀名为.java)文件,通过编译器,被翻译成一种中间代码,称为字节码(bytecode)(后缀名为.class)。Java的字节码被Java解释器解释执行。 • 可以把Java字节码看作是运行在Java虚拟机(JVM)上的机器代码指令。 Java环境 (Java平台) Java源程序 (.java) Java字节码 (.class) Java编译器 运行结果

  28. 编写Java程序 编码 遵循java语法规范编写后缀名为.java的文件。 调试 检查程序的语法和语义错误。 编译 借助Java编译工具生成可以在Java虚拟机上运行的字节码文件(.class)。 运行 运行程序 部署 把字节码文件和相关的资源文件打包,部署到相应的位置

  29. Java程序示例 public class Program{ public static void main(String[] arg){ System.out.println(“Welcome to Java!”); } }

  30. 2.1 Programming with Java • Programming with Objects • Java Program Development • First Look at Java • Element of a Java Servlet • Planning Servlet Development • Guidelines for Java Development

  31. 2.1.1 Programming with Objects • Programming with Java • Fundamentals of Object-Oriented Programming • Fundamentals of Java

  32. 参考教材内容 • 《Introduction to Programming Using Java: An Object-Oriented Approach 2nd Edition》的所需章节: • 第一章, 1.1–1.3节.

  33. Programs • Programs are texts that can make a computer do a task. • Programs are written in a specialized language, called a programming language. • The content of a program is called code. • When a computer carries out or runs a program we say that it executes the code. • Java is one of the hundreds of programming languages. It’s a object-oriented language.

  34. Programs Example • public class Program{ public static void main(String[] arg){ System.out.println(“Welcome to Java!”); } }

  35. Programs and Models(程序与模型) • A model is a simplified representation. It includes features that are considered important to its user while neglecting others. • 将实际的问题进行抽象,得到一个模型,模型包含了事物基本的重要特性。) • Every model shares following characteristics • Elements of the model represent other, more complex things. • These model elements exhibit consistent behavior. • The model elements can be grouped into different categories based on their common behaviors. • Action external to a model element cause the behavior of the model element.

  36. Example 1 • 假设有一个软件公司。该公司有一个总裁(president)和多个副总(vice president)。每个副总管理一些项目经理(project manager)。每个项目经理手下有很多程序员(Programmer)。 • How to model it ?

  37. 面向对象中的基本概念 • 对象(Object) • Java程序中模拟的元素称为对象 • 如为示例建立Java对象:一个President object、多个VicePresident object、多个Programmer object。 • 行为(Behavior) • 多个对象可能具有相同的行为。 • 如不同的程序员在不同的项目里面做不同的事情,但是他们具有一些相同的行为(common behavior):编写代码、向合作的同事索要项目需要的数据、生成项目报告等。

  38. 面向对象中的基本概念 • 消息(Message) • Each message sent must specify which object is to receive it, what task that object should perform in response, and further details that must be supplied to describe the task adequately. • 消息在不同的对象之间建立连接,使得多个对象彼此之间可以互动,即对象通过彼此间互通消息来形成互动。 • 如副总向项目经理分配任务,项目经理将任务安排给手下的多个程序员。程序员需要定期的将项目进度报告提交给项目经理。

  39. 面向对象中的基本概念 • Java程序(Java Program) • A collection of objects that correspond to the important problem elements of the problem being solved or the computation being performed. • 程序是为了完成一个任务(解决问题)的多个对象的一个集合。对象之间使用消息(message)进行联系(进行消息的传递)。

  40. 面向对象中的基本概念 • 类(Class) • A category of model elements is called a class in Java.( Java中某一类别模型元素称为类class) • The fundamental job of a Java programmer is providing class definitions, or descriptions of how objects in each class must behavior.(Java程序员的基本工作就是定义类,或描述每个类的对象的行为) • 当程序中类已经被定义了,程序运行时可以产生类(class)的多个对象(object)。每个对象称为是该类的一个实例(instance)。 • 如在example 1中,可以定义老总类,副总类,项目经理类,程序员类。项目经理类可以产生多个项目经理对象、程序员类产生多个程序员对象。

  41. 面向对象中的基本概念 • 预先定义的类(Predefined Objects and Classes) • 不同程序在执行时有许多相同的行为,如:在屏幕上打印字符。这些功能可以由预先定义好的类实现。 • Java中提供了许多这样的类,程序员在编写新的程序时可以利用这样的类和对象。

  42. Object-Oriented Programs • Java program typically consists of a group of objects that communicate by sending messages. • Java程序由一组对象组成,对象间通过消息来通信。 • When the program runs on a computer, it creates objects from class definitions provided by the Java programmer. • Java程序在计算机中运行时,它将根据程序员定义的类来创建对象。

  43. Object-Oriented Programs • A class definition for a class is a template for creating objects of that class. • 一个类的定义情况将被作为创建该类的对象实例的模板(template) • It describes how objects of that class must behave in terms of the types of messages they can receive and how they respond to those messages. • 类的定义描述了类的对象将会如何对其收到的消息产生相应的行为和反应。

  44. Example 2 • 某个公司有一个任务需要完成,该任务是要完成一个项目提案 • This task involves the Vice President of a department and the Project Manager of one of the many project teams the Vice President manages. • How to model it ?

  45. Example 2 • Need two objects to model them • an object that models the Vice President • another that models the Project Manager. • If the two objects have similar attributes and exhibit the same behavior, the program would need only one class definition to create the two objects. • If the two objects have different attributes and exhibit different behavior, the program would need two class definitions to create the two objects. • 如果两个对象在一个事务中具有相似的属性和相同的行为,那么可以把它们定义为同一个类的对象;否则将分别定义两个类来创建他们。 ——the program will need two classes to model them.

  46. Example 2 • The Vice President requests the Project Manager to write a proposal for a project. • In response to this message, the Project Manager starts writing the proposal and realizes that financial data is needed from the Vice President.

  47. Example 2 • The Project Manager asks the Vice President for financial data. • In response to this message, the Vice President sends the financial data to the Project Manager. • The Project Manager completes the proposal with the financial data and other necessary information.

  48. Example 2 • Attributes for the Vice President object: • Name • Department • Attributes for the Project Manager object: • Name • Team

  49. Example 2 • Message(s) from the Vice President object that the Project Manager object must respond to : • Write project proposal • Message(s) from the Project Manager object that the Vice President object must respond to: • Provide financial data

  50. Example 2 VicePresident ProjectManager -name -department -name -team +provideFinacialData() +writeProjectProposal() Classesto model Vice President and Project Manager

More Related