OOP with Java 通知 Project 3: 4 月 19 日晚 9 点

Similar documents
OOP with Java 通知 Project 4: 推迟至 4 月 25 日晚 9 点

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

OOP with Java 通知 Project 7, 6 月 14 日晚 9 点 本周四上机课正常进行 答疑 本周三, 周五下午 1:00 3:00, 理科楼 B911 6 月 12 日考试 ( 随堂 ) 下午 1:00-3:00 教书院 218

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课

软件工程文档编制

슬라이드 1

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点


詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

OOP with Java 通知 Project 3 提交时间 3 月 29 日晚 9 点 Piazza Project 2 投票

无类继承.key

SDK 概要 使用 Maven 的用户可以从 Maven 库中搜索 "odps-sdk" 获取不同版本的 Java SDK: 包名 odps-sdk-core odps-sdk-commons odps-sdk-udf odps-sdk-mapred odps-sdk-graph 描述 ODPS 基

内 容 简 介 本 书 是 一 本 关 于 语 言 程 序 设 计 的 教 材, 涵 盖 了 语 言 的 基 本 语 法 和 编 程 技 术, 其 中 包 含 了 作 者 对 语 言 多 年 开 发 经 验 的 总 结, 目 的 是 让 初 学 的 读 者 感 受 到 语 言 的 魅 力, 并 掌

EJB-Programming-4-cn.doc

2009年3月全国计算机等级考试二级Java语言程序设计笔试试题

使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款

佐證資料

1998目录.mdi

《大话设计模式》第一章

untitled

chp6.ppt

Microsoft Word - 新1-12.doc

《计算概论》课程 第十九讲 C 程序设计语言应用

Chapter 9: Objects and Classes

JavaIO.PDF

OOP with Java 通知 : Project 2 提交时间 : 3 月 14 日晚 9 点 另一名助教 : 王桢

OOP with Java 通知 : Project 2 提交时间 : 3 月 15 日晚 9 点

(procedure-oriented)?? 2

Viu_Cover

Viu_Cover

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

Microsoft Word - 物件導向編程精要.doc

javaexample-02.pdf

EJB-Programming-3.PDF

Chapter 9: Objects and Classes

Strings

建模与图形思考

untitled

附录J:Eclipse教程

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

Microsoft Word - 01.DOC

<4D F736F F D A67EA977B4C1A5BFA6A1A6D2B8D5B3F8A6D2C2B2B3B92E646F6378>

(6) 要 求 付 款 管 理 员 从 预 订 表 中 查 询 距 预 订 的 会 议 时 间 两 周 内 的 预 定, 根 据 客 户 记 录 给 满 足 条 件 的 客 户 发 送 支 付 余 款 要 求 (7) 支 付 余 款 管 理 员 收 到 客 户 余 款 支 付 的 通 知 后, 检

Microsoft PowerPoint - plan08.ppt

基于ECO的UML模型驱动的数据库应用开发1.doc

Java Access 5-1 Server Client Client Server Server Client 5-2 DataInputStream Class java.io.datainptstream (extends) FilterInputStream InputStream Obj

3.1 num = 3 ch = 'C' 2

非 洲 之 心 與 來 自 北 美 的 成 功 商 人 正 在 合 作 開 發 迦 南 美 地, 他 們 和 來 自 肯 亞 史 瓦 濟 蘭 的 傑 出 經 驗 豐 富 且 受 過 教 育 的 業 務 人 員 一 起 工 作, 一 同 創 造 一 個 獨 特 的 量 身 訂 造 的 組 織, 去 滿

模板

untitled

電機工程系認可證照清單 /7/1

IoC容器和Dependency Injection模式.doc

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

Microsoft Word - 先玉335 copy.doc

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

Java 1 Java String Date

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

Microsoft PowerPoint - L17_Inheritance_v4.pptx

融通开放式基金月刊(2009年4月).doc

Microsoft Word - 特种水产饲料行业研究报告.docx

1.5招募说明书(草案)

2. AOP 底层技术实现 小风 Java 实战系列教程 关键词 : 代理模式 代理模型分为两种 : 1) 接口代理 (JDK 动态代理 ) 2) 子类代理 (Cglib 子类代理 ) 需求 :CustomerService 业务类, 有 save,update 方法, 希望在 save,updat

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Microsoft Word - Final Exam Review Packet.docx

JBuilder Weblogic

<4D F736F F D20C8EDC9E82DCFC2CEE7CCE22D3039C9CF>

Microsoft Word - A _ doc

建模与图形思考

Thinking in Java 3rd Edition

Lek Yuen General Out-Patient Clinic 9 ( : ) G/F, Lek Yuen Health Centre, 9 Lek Yuen Street, Shatin (Tel ) Monday to Friday Saturday


本章学习目标 小风 Java 实战系列教程 SpringMVC 简介 SpringMVC 的入门案例 SpringMVC 流程分析 配置注解映射器和适配器 注解的使用 使用不同方式的跳转页面 1. SpringMVC 简介 Spring web mvc

Microsoft Word - ch04三校.doc

untitled

ebook55-13

untitled

<4D F736F F F696E74202D20B5DA3035D5C220C3E6CFF2B6D4CFF3B8DFBCB6B3CCD0F2C9E8BCC6>

Ref: A ( 三 ) 升 留 級 準 則 本 校 對 學 生 的 學 術 水 平 有 嚴 格 要 求, 學 生 必 須 達 到 指 定 標 準, 才 可 升 級 細 則 如 下 : 初 中 升 級 條 件 ( 全 年 總 成 績 須 符 合 以 下 各 項, 方 准 予 升 級 )

1: public class MyOutputStream implements AutoCloseable { 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6:

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

D C 93 2

使用MapReduce读取XML文件

提问袁小兵:

編 者 的 話 甄 選 改 變 大 學 與 高 中 職 校 園 亞 洲 國 家 強 調 以 筆 試 作 為 重 要 入 學 憑 據, 歐 美 國 家 則 傾 向 學 校 選 學 生 和 學 生 選 科 系 以 美 國 史 丹 佛 大 學 為 例, 全 面 採 取 個 人 申 請 入 學 制, 每 年

Microsoft Word - Broker.doc

1:30 PM Site Visit 参 观 1 Session 4 课 程 4 Session 6 课 程 6 Site visit 参 观 2 Hotel Check-in and have a brief ur of downwn Vancouver 入 住 酒 店, 简 单 参 观 温 哥

Microsoft Word - 第5章.doc

Guava学习之Resources

java2d-4.PDF

Import Sailing Schedule DK - Signe.xlsx

Java

The Embedded computing platform

第八章类和对象的扩展 实验目的 (1) 类的继承 a. 掌握父类及子类的关系及创建方法 b. 掌握上转型对象的使用方法 c. 掌握子类对象创建过程中与父类构造方法的关系, 及 super 关键字的用法 (2) 多态与接口 a. 掌握抽象类与抽象方法的概念 b. 掌握多态的实现原理及方法 c. 了解成

内 容 提 要 将 JAVA 开 发 环 境 迁 移 到 Linux 系 统 上 是 现 在 很 多 公 司 的 现 实 想 法, 而 在 Linux 上 配 置 JAVA 开 发 环 境 是 步 入 Linux 下 JAVA 程 序 开 发 的 第 一 步, 本 文 图 文 并 茂 地 全 程 指

管道建模基础.ppt

Transcription:

OOP with Java Yuanbin Wu cs@ecnu

OOP with Java 通知 Project 3: 4 月 19 日晚 9 点

复习 Upcasting 同一基类的不同子类可以被视为同一类型 ( 基类 ) 放宽类型一致性 简化接口 class A{ class B{ A a = new A(); B b = new B(); // A a = new B(); compile error class A{ class B extends A{ A a = new A(); B b = new B(); A a = new B(); // upcasting

复习 多态 class Instrument { public void play(int note) { System.out.println( Instrument.play() + n); public class Wind extends Instrument { public void play(int note) { System.out.println( Wind.play() + n); public class Music { public static void tune(instrument i) { i.play(); public static void main(string []args){ Wind flute = new Wind(); Stringed violin = new Stringed(); Brass frenchhorn = new Brass(); tune(flute); tune(violin); tune(frenchhorn); public class Stringed extends Instrument { public void play(int note) { System.out.println( Stringed.play() + n); public class Brass extends Instrument { public void play(int note) { System.out.println( Brass.play() + n); 多态 (Polymorphism) 参数 Instrument I 可以代表不同的子类, 并能正确调用它们的方法 ( 即, 有多种表现形态 )

复习 静态绑定 函数的调用在编译后便确定, 也称为 early binding 优点 : 快速, 易于 debug, 缺点 : 接口繁琐 动态绑定 函数的调用在运行时才能确定也称 late binding 优点 : 接口简洁缺点 : 函数调用需要额外开销 Java 中的所有方法都采用动态绑定, 除了 final Static 数据成员不动态绑定

OOP with Java 抽象类 接口 定义 实现多个接口 扩展接口 接口适配器 应用 : 工厂模式

抽象类 父类的方法 Instrument: play() 子类重写父类的方法 Wind: play() Stringed: play() Brass: play() 可扩展性 用户程序仅知父类方法 子类修改不会影响用户程序 play() Wind Instrument play() Stringed play() play() Brass public class Music { public static void tune(instrument i) { i.play(); public static void main(string []args){ Wind flute = new Wind(); Stringed violin = new Stringed(); Brass frenchhorn = new Brass(); tune(flute); tune(violin); tune(frenchhorn);

抽象类 如果所有子类都将重写该方法 Instrument: play()?? 是否有机制 : 在父类中不指定该方法的具体实现 禁止调用父类的该方法 Instrument play() play() Wind Stringed play() play() Brass

抽象类 抽象方法 (abstract method) 仅提供方法的名称, 参数和返回值 没有具体实现 使用 abstract 关键字 class Instrument { public void play(int note) { System.out.println( Instrument.play() + n); abstract class Instrument { public abstract void play(int note) ; 普通方法 抽象方法

抽象类 抽象类 (abstract class) 包含抽象方法的类称为抽象类

抽象类 抽象类 abstract class Instrument { public abstract void play(int note) ; public class Wind extends Instrument { public void play(int note) { System.out.println( Wind.play() + n); public class Stringed extends Instrument { public void play(int note) { System.out.println( Stringed.play() + n); public class Brass extends Instrument { public void play(int note) { System.out.println( Brass.play() + n);

抽象类 是否能创建抽象类的对象? 否 抽象类是不完整的类 其中的抽象方法需要在子类补充完整 ( 重写 ) 后才有意义

抽象类 无法创建该类的对象 abstract class Instrument { public abstract void play(int note) ; class Test { public static void main(string []args){ // Instrument in = new Instrument(); // compile error: can not create instances of an abstract class

抽象类 abstract Instrument abstract void play(); String what() { /* */ extends Wind Stringed Percussion void play() String what() void play() String what() void play() String what() extends Woodwind void play() String what() Brass void play() String what()

abstract class Instrument { public abstract void play(int note) ; public String what() {return Instrument ; class Stringed extends Instrument { public void play(int note) { System.out.println( Stringed.play() + n); public String what() {return Stringed ; public class Music { public static void tune(instrument i) { i.play(); public static void main(string []args){ Wind flute = new Wind(); Stringed violin = new Stringed(); Brass frenchhorn = new Brass(); tune(flute); tune(violin); tune(frenchhorn); class Percussion extends Instrument { public void play(int note) { System.out.println( Percussion.play() + n); public String what() {return Percussion ; abstract class Wind extends Instrument { public abstract void play(int note) ; public String what() {return Wind ; 若子类没有重写父类中的抽象方法, 子类仍为抽象类 class Woodwind extends Wind { public void play(int note) { System.out.println( Woodwind.play() + n); public String what() {return Woodwind ; class Brass extends Wind { public void play(int note) { System.out.println( Brass.play() + n); public String what() {return Brass ;

抽象类 总结 抽象类包含抽象方法, 只有方法名, 参数, 返回值, 没有方法的实现 抽象类不能实例化 若子类没有重写父类中的抽象方法, 子类仍为抽象类

接口 接口 定义 实现多个接口 扩展接口 接口适配器 应用 : 工厂模式

接口 抽象类 抽象方法 普通方法 abstract class Instrument { public abstract void play(int note) ; public String what() {return Instrument ;

接口 接口 (Interface) 所有方法都是抽象方法 只有方法的名称, 参数和返回值 没有方法的实现 abstract class Instrument { public abstract void play(int note); public abstract String what(); interface Instrument { void play(int note) ; String what();

接口 继承 重用 (class reusing) 子类重用父类的方法 / 数据 upcasting 和多态 接口 父类出现之处可用子类代替 能够调用正确的子类方法 ( 动态绑定 ) 没有代码重用, 仅仅保留 upcasting 和多态

接口 接口 所有实现该接口的类都具有接口提供的方法 任何使用该接口类型的方法, 都可以使用他的任何一种实现 某种协议 (protocol)

接口 接口的实现 接口 : 方法长什么样? 实现 : 方法具体怎样工作?

接口 接口的实现 abstract class Instrument { public abstract void play(int note); public abstract String what(); interface Instrument { void play(int note) ; String what(); class Stringed extends Instrument { public void play(int note) { System.out.println( Stringed.play() + n); public String what() {return Stringed ; class Stringed implements Instrument { public void play(int note) { System.out.println( Stringed.play() + n); public String what() {return Stringed ; 继承 : 1. extends 关键字 2. 父类, 子类关系 3. class, extends 接口 : 1. implements 关键字 2. 接口, 实现关系 3. interface, implements

interface Instrument void play(); String what(); implements Wind Stringed Percussion void play() String what() void play() String what() void play() String what() extends Woodwind void play() String what() Brass void play() String what()

interface class Instrument { void play(int note) ; String what(); class Stringed implements Instrument { public void play(int note) { System.out.println( Stringed.play() + n); public String what() {return Stringed ; public class Music { public static void tune(instrument i) { i.play(); public static void main(string []args){ Wind flute = new Wind(); Stringed violin = new Stringed(); Brass frenchhorn = new Brass(); tune(flute); tune(violin); tune(frenchhorn); class Percussion implements Instrument { public void play(int note) { System.out.println( Percussion.play() + n); public String what() {return Percussion ; class Wind implements Instrument { public void play(int note) { System.out.println( Wind.play() + n); public String what() {return Wind ; 普通类, 抽象类, 接口 class Woodwind extends Wind { public void play(int note) { System.out.println( Woodwind.play() + n); public String what() {return Woodwind ; class Brass extends Wind { public void play(int note) { System.out.println( Brass.play() + n); public String what() {return Brass ;

接口 接口 所有方法默认为 public interface Instrument { void play(int note) ; String what(); interface Instrument { public void play(int note) ; public String what();

接口 接口 所有数据默认为 final static 定义常量 interface Week { int MONDAY = 1; int TUESDAY = 2; int WEDNESDAY = 3; int THURSDAY = 4; int FRIDAY = 5; int SATURDAY = 6; int SUNDAY = 7; class Week { public static final int MONDAY = 1; public static final int TUESDAY = 2; public static final int WEDNESDAY = 3; public static final int THURSDAY = 4; public static final int FRIDAY = 5; public static final int SATURDAY = 6; public static final int SUNDAY = 7;

接口 接口 定义 实现多个接口 扩展接口 接口适配器 应用 : 工厂模式

接口 一个类实现多个接口 Interface Plane void fly() Interface Boat void sail() implements Seaplane void fly() void sail()

接口 一个类实现多接口 interface Plane { void fly(); interface Boat { void sail(); class Seaplane implements Plane, Boat { public void fly(){ System.out.println( Fly! ); public void sail(){ System.out.println( Sail! );

接口 问题 : 如果将接口替换成普通类会如何? Interface Plane Interface Boat Plane Boat void fly() void sail() void fly() void sail() implements? extends Seaplane Seaplane void fly() void sail() void fly() void sail()

接口 多继承问题 Diamond problem Transportation void forward() void backward() Seaplane s = new Seaplane(); // s.forward() which one? extends Plane void forward() void backward() void fly() Boat void forward() void backward() void sail() extends Seaplane void forward() void backward() void fly() void sail()

接口 多继承问题 父类只能有一个普通类 / 抽象类 class A { class B { /* error class C extends A, B { */

接口 Abstract or Concrete Base class interface 1 interface 2 interface n Base class methods interface 1 interface 2... interface n Methods of the derived class

interface CanFight { void fight(); interface CanSwim { void swim(); interface CanFly { void fly(); class ActionCharacter { public void fight() { class Hero extends ActionCharacter Implements CanFight, CanSwim, CanFly{ public void fly() { public void swim() { public class Adventure { public static void t(canfight x) { x.fight(); public static void u(canswim x) { x.swim(); public static void v(canfly x) { x.fly(); public static void w(actioncharacter x) { x.fight(); public static void main(string []args) { Hero h = new Hero(); t(h); u(h); v(h); w(h); 1. 实现多个接口可以 upcast 到不同的类 2. abstract class or interface?

接口 实现多个接口 名字冲突 interface I1 { void f(); class C1 implements I1, I2{ public void f() { interface I2 { void f(); interface I3 { void f(int i); interface I4 { int f(); class C2 implements I1, I3{ public void f() { public void f(int i) { /* compile error: return type incompatible class C2 implements I1, I4{ public void f() { */

接口 接口 定义 实现多个接口 扩展接口 接口适配器 应用 : 工厂模式

接口 扩展接口 interface A { interface B extends A{ interface D { interface D extends A, C{

interface Monster { void menace(); interface DangerousMonster extends Monster{ void destroy(); class DragonZilla implements DangerousMonster{ public void menace() { public void destroy() { public class HorrorShow { public static void u(monster x) { x.menace(); public static void v(dangerousmonster x) { x.menace(); x.destroy(); public static void w(lethal x) { x.kill(); public static void main(string []args) { DangerousMonster m = DangerousZilla(); u(m); v(m); Vampire a = VeryBadVampire(); u(a); v(a); w(a); interface Lethal { void kill(); interface Vampire extends DangerousMonster, Lethal{ void drinkblood(); class VeryBadVampire implements Vampire{ public void menace() { public void destroy() { public void kill() { public void drinkblood() {

接口 接口 定义 实现多个接口 扩展接口 接口适配器 应用 : 工厂模式

接口 接口适配器 (Adapter) 方法 f(interface1 i) 类 A 已经写好, 但在并没有实现 Interface1 接口 希望方法 f() 能处理类 A 的对象 复用方法 f() 的代码

interface CanFly { void fly(); class Bird implements CanFly{ public void fly() { class Person { public void walk(){ public void buyticket(){ public void takeflight(){ class Insect implements CanFly{ public void fly() {? class Adventure { public static void travel(canfly c) { c.fly(); public static void main(string []args){ Bird b = new Bird(); Insect ins = new Insect(); travel(b); travel(ins);

interface CanFly { void fly(); class Person { public void walk(){ public void buyticket(){ public void takeflight(){ class Adventure { public static void travel(canfly c) { c.fly(); public static void main(string []args){ Bird b = new Bird(); Insect ins = new Insect(); travel(b); travel(ins); Person p = new Person(); PersonAdapter pd = new PersonAdapter(p); travel(pd); class PersonAdapter implements CanFly{ private Person p; public PersonAdapter(Person p{ this.p = p; public void fly(){ p.buyticket(); p.takeflight();

接口 接口适配器 通过增加一个接口的实现, 使得现有类能够被 适配 到该接口

接口 接口适配器 Existing class (Person) Adapter (PersonAdapter) Existing interface (CanFly)

接口 接口 定义 实现多个接口 扩展接口 接口适配器 应用 : 工厂模式

接口 应用 : 工厂模式 更灵活的构造对象方式

interface Service { void method1(); void method2(); class Impl1 implements Service { public void method1() { System.out.println( Imp1.method1 ); public void method2() { System.out.println( Imp1.method2 ); public class TestService { public static void consume(service s) { s.method1(); s.method2(); public static void main(string []args){ Service s1 = new Impl1(); Service s2 = new Impl2(); consume(s1); consume(s2); class Impl2 implements Service { public void method1() { System.out.println( Imp2.method1 ); public void method2() { System.out.println( Imp2.method2 ); 当构造对象 / 初始化比较繁琐时, 可以增加一层包装

interface Service { void method1(); void method2(); class Impl1 implements Service { public void method1() { System.out.println( Imp1.method1 ); public void method2() { System.out.println( Imp1.method2 ); class Impl2 implements Service { public void method1() { System.out.println( Imp2.method1 ); public void method2() { System.out.println( Imp2.method2 ); interface ServiceFactory { Service getservice(); class Impl1Factory implements ServiceFactory { public Service getservice() { return new Impl1(); class Impl2Factory implements ServiceFactory { public Service getservice() { return new Impl2(); public class TestService { public static void consume(servicefactory sf) { Service s = sf.getservice(); s.method1(); s.method2(); public static void main(string []args){ ServiceFactory sf1 = new Impl1Factory(); ServiceFactory sf2 = new Impl2Factory(); consume(sf1); consume(sf2);

总结 抽象类 抽象方法 : 只给出方法的名字, 参数, 返回值, 没有具体实现 抽象类 : 包含抽象方法的类 abstract 关键字 接口 所有的方法都是抽象方法 interface, inplements 关键字 接口的扩展 : extends 实现多个接口 接口适配器