Java 程序设计 Java Programming Fall, 2013. Chapter 7 Interfaces 2/44 Contents Single vs. Multiple Inheritance Abstract Classes Interface.

Slides:



Advertisements
Similar presentations
第十二章 常微分方程 返回. 一、主要内容 基本概念 一阶方程 类 型 1. 直接积分法 2. 可分离变量 3. 齐次方程 4. 可化为齐次 方程 5. 全微分方程 6. 线性方程 类 型 1. 直接积分法 2. 可分离变量 3. 齐次方程 4. 可化为齐次 方程 5. 全微分方程 6. 线性方程.
Advertisements

第 12 章位运算 C 语言兼具高级语言及低级语言的特性,因此 适合编写系统软件。 C 语言具备低级语言的特性 就在于它能直接对硬件进行操作,即位运算。 所谓位运算是指,按二进制位进行的运算。 例如,将一个存储单元中各二进位左移或右移一 位等。
HistCite 结果分析示例 罗昭锋. By:SC 可能原因:文献年度过窄,少有相互引用.
一、拟合优度检验 二、变量的显著性检验 三、参数的置信区间
编译程序 构造原理和实现技术 授课教师:吕江花. 第一章 编译程序概述 主要内容: 几个基本概念 编译器的工作过程概述 编译器各个阶段的功能描述 编译程序的实现途径.
嵌入式操作系统 陈香兰 Fall 系统调用 10/27/09 嵌入式 OS 3/12 系统调用的意义  操作系统为用户态进程与硬件设备进行交互提供 了一组接口 —— 系统调用  把用户从底层的硬件编程中解放出来  极大的提高了系统的安全性  使用户程序具有可移植性.
地理信息系统概述. 数据和信息 (Data & Information) 数据 原始事实 如:员工姓名, 数据可以有数值、图形、声音、视觉数据等 信息 以一定规则组织在一起的事实的集合。
第 4 章 抽象解释 内容概述 以一种独立于编程语言的方式,介绍抽象解释的 一些本质概念 – 将 “ 程序分析对语言语义是正确的 ” 这个概念公式 化 – 用 “ 加宽和收缩技术 ” 来获得最小不动点的较好的 近似,并使所需计算步数得到限制 – 用 “ 伽罗瓦连接和伽罗瓦插入 ” 来把代价较大的属 性空间用代价较小的属性空间来代替.
第 2 章 数据流分析 内容概述 数据流分析推导的是数据沿着程序执行路 径流动的信息 – 过程内的分析:可用表达式分析、到达-定值分 析等 – 过程间分析 –Shape 分析 – 理论基础 – 数据流方程的求解.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第六十二讲 ) 离散数学. 最后,我们构造能识别 A 的 Kleene 闭包 A* 的自动机 M A* =(S A* , I , f A* , s A* , F A* ) , 令 S A* 包括所有的 S A 的状态以及一个 附加的状态 s.
分析化学与无机化学中溶液 pH 值计算的异同比较 谢永生  分析化学是大学化学系的一门基础课,课 时较少,其内容主要是无机物的化学分析。 分析化学是以无机化学作为基础的,我们 都是在已掌握一定的无机化学知识后才学 习分析化学 。所以在分析 化学的学习中会 重复许多无机化学内容,造成学习没有兴.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
1 为了更好的揭示随机现象的规律性并 利用数学工具描述其规律, 有必要引入随 机变量来描述随机试验的不同结果 例 电话总机某段时间内接到的电话次数, 可用一个变量 X 来描述 例 检测一件产品可能出现的两个结果, 也可以用一个变量来描述 第五章 随机变量及其分布函数.
计算机文化基础 第 13 章 多表操作. 多表操作 以前所进行的操作中,在同一时刻只能打开一个表文 件,这是单工作区操作。但是在有些情况下,我们需要同时 了解多个表文件中的内容,例如 图 8-1 。在表文件 Stud1.DBF 中,有姓名,班级,电话三项;在 Stud2.DBF 中, 有姓名,性别,籍贯,英语四个字段。在单工作区操作方式.
例9:例9: 第 n-1 行( -1 )倍加到第 n 行上,第( n-2 ) 行( -1 )倍加到第 n-1 行上,以此类推, 直到第 1 行( -1 )倍加到第 2 行上。
主讲教师:陈殿友 总课时: 124 第八讲 函数的极限. 第一章 机动 目录 上页 下页 返回 结束 § 3 函数的极限 在上一节我们学习数列的极限,数列 {x n } 可看作自变量 为 n 的函数: x n =f(n),n ∈ N +, 所以,数列 {x n } 的极限为 a, 就是 当自变量 n.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第三十八讲 ) 离散数学. 第八章 格与布尔代数 §8.1 引 言 在第一章中我们介绍了关于集 合的理论。如果将 ρ ( S )看做 是集合 S 的所有子集组成的集合, 于是, ρ ( S )中两个集合的并 集 A ∪ B ,两个集合的交集.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第四十八讲 ) 离散数学. 例 设 S 是一个非空集合, ρ ( s )是 S 的幂集合。 不难证明 :(ρ(S),∩, ∪,ˉ, ,S) 是一个布尔代数。 其中: A∩B 表示 A , B 的交集; A ∪ B 表示 A ,
第十一章 曲线回归 第一节 曲线的类型与特点 第二节 曲线方程的配置 第三节 多项式回归.
第二部分 行政法律关系主体 第一节 行政主体 一、行政主体 (一)行政主体的概念 cc (二)行政主体资格含义及构成要件 CASE1CASE1\CASE2CASE2 (三)行政主体的职权和职责 1 、行政职权的概念及内容 2 、行政职权的特点 3 、行政职责.
第三章 C 数据及其表达式 2 回顾  程序是为执行一项任务而编写的有序指令集  算法是解决问题的具体方法和步骤  流程图是算法的图形化表现形式  C 程序的编译和运行过程  编译器将源程序转换成机器能理解的程序  连接器用于连接相关的目标文件以生成可执行程序  进制间的转换  补码.
线性代数习题课 吉林大学 术洪亮 第一讲 行 列 式 前面我们已经学习了关 于行列式的概念和一些基本 理论,其主要内容可概括为:
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第二十五讲 ) 离散数学. 定理 群定义中的条件 ( 1 )和( 2 )可以减弱如下: ( 1 ) ’ G 中有一个元素左壹适合 1 · a=a; ( 2 ) ’ 对于任意 a ,有一个元素左逆 a -1 适 合 a -1 ·
第 4 章 过程与变量的作用范围. 4.1 Visual Basic 的代码模块 Visual Basic 的应用程序是由过程组成的, 过程代码存放在模块中。 Visual Basic 提供了 三类模块,它们是窗体模块、标准模块和类 模块。 窗体模块 窗体模块是大多数 Visual Basic.
数 学 系 University of Science and Technology of China DEPARTMENT OF MATHEMATICS 第 3 章 曲线拟合的最小二乘法 给出一组离散点,确定一个函数逼近原函数,插值是这样的一种手段。 在实际中,数据不可避免的会有误差,插值函数会将这些误差也包括在内。
自顶向下分析 —— 递归下降法 递归下降法 (Recursive-Descent Parsing) 对每个非终极符按其产生式结构产生相应语 法分析子程序. 终极符产生匹配命令 非终极符则产生调用命令 文法递归相应子程序也递归,所以称这种方 法为递归子程序方法或递归下降法。
聚合物在生物高分子分离中的应用 王延梅 中国科学技术大学高分子科学与工程系 Tel
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第三十九讲 ) 离散数学. 例 设 S 是一个集合, ρ ( S )是 S 的幂集合,集合 的交( ∩ ),并(∪)是 ρ ( S )上的两个代数运算, 于是,( ρ ( S ), ∩ ,∪) 是一个格。而由例 知.
语义分析 主要内容:  语义分析概述 ( 必要性、功能、描述方法 )  符号表  类型表达式  声明和程序体的语义分析.
1 第 7 章 专家控制系统 概述 专家系统的起源与发展 专家系统的一般结构 专家系统的知识表示和获取 专家系统的特点及分类.
信息利用与学术论文写作 Library of Jiangsu University, Zhenjiang Sha Zhenjiang
非均相物系的分离 沉降速度 球形颗粒的 :一、自由沉降 二、沉降速度的计算 三、直径计算 1. 试差法 2. 摩擦数群法 四、非球形颗粒的自由沉降 1. 当量直径 de :与颗粒体积相等的圆球直径 V P — 颗粒的实际体积 2. 球形度  s : S—— 与颗粒实际体积相等的球形表面积.
量子化学 第四章 角动量与自旋 (Angular momentum and spin) 4.1 动量算符 4.2 角动量阶梯算符方法
数 学 系 University of Science and Technology of China DEPARTMENT OF MATHEMATICS 第 5 章 解线性方程组的直接法 实际中,存在大量的解线性方程组的问题。很多数值方 法到最后也会涉及到线性方程组的求解问题:如样条插值的 M 和.
主讲教师:陈殿友 总课时: 124 第十一讲 极限的运算法则. 第一章 二、 极限的四则运算法则 三、 复合函数的极限运算法则 一 、无穷小运算法则 机动 目录 上页 下页 返回 结束 §5 极限运算法则.
在发明中学习 线性代数 概念的引入 李尚志 中国科学技术大学. 随风潜入夜 : 知识的引入 之一、线性方程组的解法 加减消去法  方程的线性组合  原方程组的解是新方程的解 是否有 “ 增根 ” ?  互为线性组合 : 等价变形  初等变换  高斯消去法.
Photoshop CS4 标准培训教程 第三章第三章 在 Photoshop CS4 中所谓的不规则选区指的是随意性强,不被局限在几何形状内, 他们可以是鼠标任意创建的也可以是通过计算而得到的单个选区或多个选区。在 Photoshop 中可以用来创建不规则选区的工具被分组放置到套索工具组、魔棒工具组.
§2.2 一元线性回归模型的参数估计 一、一元线性回归模型的基本假设 二、参数的普通最小二乘估计( OLS ) 三、参数估计的最大或然法 (ML) 四、最小二乘估计量的性质 五、参数估计量的概率分布及随机干 扰项方差的估计.
第一节 相图基本知识 1 三元相图的主要特点 (1)是立体图形,主要由曲面构成; (2)可发生四相平衡转变; (3)一、二、三相区为一空间。
9的乘法口诀 1 .把口诀说完全。 二八( ) 四六( ) 五八( ) 六八( ) 三七( ) 三八( ) 六七( ) 五七( ) 五六( ) 十六 四十八 四十二 二十四 二十一 三十五 四十 二十四 三十 2 .口算, 并说出用的是哪句口诀。 8×8= 4×6= 7×5= 6×8= 5×8=
量子力学教程 ( 第二版 ) 3.4 连 续 谱 本 征 函 数 的 归 一 化 连续谱本征函数是不能归一化的 一维粒子的动量本征值为的本征函数 ( 平面波 ) 为 可以取 中连续变化的一切实数值. 不难看出,只要则 在量子力学中, 坐标和动量的取值是连续变化 的 ; 角动量的取值是离散的.
第 3 章 控制流分析 内容概述 – 定义一个函数式编程语言,变量可以指称函数 – 以 dynamic dispatch problem 为例(作为参数的 函数被调用时,究竟执行的是哪个函数) – 规范该控制流分析问题,定义什么是可接受的控 制流分析 – 定义可接受分析在语义模型上的可靠性 – 讨论分析算法.
吉林大学远程教育课件 主讲人 : 杨凤杰学 时: 64 ( 第五十三讲 ) 离散数学. 定义 设 G= ( V , T , S , P ) 是一个语法结构,由 G 产生的语言 (或者说 G 的语言)是由初始状态 S 演绎出来的所有终止符的集合, 记为 L ( G ) ={w  T *
平行线的平行公理与判定 九年制义务教育七年级几何 制作者:赵宁睿. 平行线的平行公理与判定 要点回顾 课堂练习 例题解析 课业小结 平行公理 平行判定.
Relevance Theory Lecture 12. Relevance Theory 交际研究的对象是交际的概念、内容、 性质、功能、方法和交际行为、交际参 加者之间的关系等的认识和阐述。 20 世 纪 70 年代末以来, Sperber & Wilson 把认 知与交际结合起来,于 1986.
编译原理总结. 基本概念  编译器 、解释器  编译过程 、各过程的功能  编译器在程序执行过程中的作用  编译器的实现途径.
周期信号的傅里叶变换. 典型非周期信号 ( 如指数信号, 矩形信号等 ) 都是满足绝对可 积(或绝对可和)条件的能量信号,其傅里叶变换都存在, 但绝对可积(或绝对可和)条件仅是充分条件, 而不是必 要条件。引入了广义函数的概念,在允许傅里叶变换采用 冲激函数的前提下, 使许多并不满足绝对可积条件的功率.
 符号表  标识符的作用: 声明部分:定义了各种对象及对应的属性和 使用规则。 程序体:对所定义的对象进行各种操作。 $ididname IdnameAttributeIR  必要性 Token : 新表-符号表(种类、类型等信息):
Department of Mathematics 第二章 解析函数 第一节 解析函数的概念 与 C-R 条件 第二节 初等解析函数 第三节 初等多值函数.
1 第 7 章 存储过程、触发器和程序包 在很多时候,都需要保存 PL/SQL 程序块,以便 随后可以重新使用。这也意味着,程序块需要一个名 称,这样需才可以调用或者引用它。命名的 PL/SQL 程序块可被独立编译并存储在数据库中,任何与数据 库相连接的应用程序都可以访问这些存储的 PL/SQL 程序块。
《 UML 分析与设计》 交互概述图 授课人:唐一韬. 知 识 图 谱知 识 图 谱知 识 图 谱知 识 图 谱.
Introduction to Automatic Control The Laplace Transform Li Huifeng Tel:
1 、如果 x + 5 > 4 ,那么两边都 可得 x >- 1 2 、在- 3y >- 4 的两边都乘以 7 可得 3 、在不等式 — x≤5 的两边都乘以- 1 可得 4 、将- 7x — 6 < 8 移项可得 。 5 、将 5 + a >- 2 a 移项可得 。 6 、将- 8x < 0.
Java 水晶报表. 目录 水晶报表开发 (CrystalReports 2008) Java 水晶报表 (JBuilder 2005) 报表项目布署 (WebSphere v6.1)
§10.2 对偶空间 一、对偶空间与对偶基 二、对偶空间的有关结果 三、例题讲析.
请同学们仔细观察下列两幅图有什么共同特点? 如果两个图形不仅形状相同,而且每组对应点所在的直线 都经过同一点, 那么这样的两个图形叫做位似图形, 这个点叫做位 似中心.
表单自定义 “ 表单自定义 ” 功能是用于制作表单的 工具,用数飞 OA 提供的表单自定义 功能能够快速制作出内容丰富、格 式规范、美观的表单。
力的合成 力的合成 一、力的合成 二、力的平行四边形 上一页下一页 目 录 退 出. 一、力的合成 O. O. 1. 合力与分力 我们常常用 一个力来代替几个力。如果这个 力单独作用在物体上的效果与原 来几个力共同作用在物体上的效 果完全一样,那么,这一个力就 叫做那几个力的合力,而那几个 力就是这个力的分力。
河南济源市沁园中学 前进中的沁园中学欢迎您 ! 温故知新: 1 、什么是原子? 2 、原子是怎样构成的? 3 、原子带电吗?为什么?
Java 程序设计 Java 程序设计 Java Programming Spring, 2013.
个体 精子 卵细胞 父亲 受精卵 母亲 人类生活史 问题:人类产生配子(精、卵 细胞)是不是有丝分裂?
逻辑设计基础 1 第 7 章 多级与(或)非门电路 逻辑设计基础 多级门电路.
八. 真核生物的转录 ㈠ 特点 ① 转录单元为单顺反子( single cistron ),每 个蛋白质基因都有自身的启动子,从而造成在功能 上相关而又独立的基因之间具有更复杂的调控系统。 ② RNA 聚合酶的高度分工,由 3 种不同的酶催化转 录不同的 RNA 。 ③ 需要基本转录因子与转录调控因子的参与,这.
§5.6 利用希尔伯特 (Hilbert) 变换 研究系统的约束特性 希尔伯特变换的引入 可实现系统的网络函数与希尔伯特变换.
1 第三章 数列 数列的概念 考点 搜索 ●数列的概念 ●数列通项公式的求解方法 ●用函数的观点理解数列 高考 猜想 以递推数列、新情境下的 数列为载体, 重点考查数列的通 项及性质, 是近年来高考的热点, 也是考题难点之所在.
第二节. 广告牌为什么会被风吹倒? 结构的稳定性: 指结构在负载的作用下 维持其原有平衡状态的能力。 它是结构的重要性质之一。
目录 上页 下页 返回 结束 二、无界函数反常积分的审敛法 * 第五节 反常积分 无穷限的反常积分 无界函数的反常积分 一、无穷限反常积分的审敛法 反常积分的审敛法  函数 第五章 第五章.
§7.2 估计量的评价标准 上一节我们看到,对于总体 X 的同一个 未知参数,由于采用的估计方法不同,可 能会产生多个不同的估计量.这就提出一 个问题,当总体的一个参数存在不同的估 计量时,究竟采用哪一个好呢?或者说怎 样评价一个估计量的统计性能呢?下面给 出几个常用的评价准则. 一.无偏性.
Interface & Inner classes
Chapter 6 Reusing Classes.
Presentation transcript:

Java 程序设计 Java Programming Fall, 2013

Chapter 7 Interfaces 2/44 Contents Single vs. Multiple Inheritance Abstract Classes Interface

Chapter 7 Interfaces 3/44 Single vs. Multiple Inheritance Some object-oriented languages (eg.: C++) allow multiple( 多重的 ) inheritance, which allows a class to be derived from two or more classes, inheriting the members of all parents; The price( 代价 ): collisions( 冲突 ), such as the same variable name, same method name in two parents, have to be resolved;

Chapter 7 Interfaces 4/44 Single vs. Multiple Inheritance Java decision: single inheritance for classes, meaning that a derived class can have only one parent class; However, in most cases, the use of interfaces( 接口 ) gives us aspects of multiple inheritance without the overhead (will discuss later).

Chapter 7 Interfaces 5/44 abstract Classes( 抽象类 ) An abstract class is a conceptual class ,表示的是 “ is-a ” 关 系. abstract class provides a common root for a group of classes --共性 ; use the modifier abstract on a class header to declare an abstract class: abstract class className { …… }

Chapter 7 Interfaces 6/44 Inheritance ( 继承 ) Example: Book NovelDictionary MysteryRomance

Chapter 7 Interfaces 7/44 Abstract Classes ( 抽象类 ) An abstract class often contains abstract methods, though it doesn ’ t have to  abstract methods consist of only methods declarations, without any method body. When a class contains one or more abstract methods, it should be declared as abstract class; The abstract methods of an abstract class must be defined in its subclass; We cannot declare abstract constructors or abstract static methods.

8 Abstract Classes abstract class ClassName { //variables … abstract dataType MethodName1(); // 抽象方法 … dataType Method2() { // method body } 已实现的方法, 即:非抽象方法

9 //Animal.java public abstract class Animal { public String name; public int age; public void print() { System.out.println(" 名字 :"+name); System.out.println(" 大小 :"+age); } public abstract void run(); public abstract void cry(); }

Chapter 7 Interfaces 10/44 Abstract Classes ( 抽象类 ) public class AAnimal // 错误 { public String name; public int age; public void print() { System.out.println(" 名字 :"+name); System.out.println(" 大小 :"+age); } public abstract void run(); public abstract void cry(); } public abstract class AAnimal 修改为

Chapter 7 Interfaces 11/44 Abstract Classes( 抽象类 ) 实例化- 由类创建对象 / 实例. An abstract class cannot be instantiated ( 实例化 ) : objects cannot be created( 对象不能被创建 ); 它只能做为父类使用,由它派生的子类必须实现抽象类中所 有的抽象方法,才能创建对象。 在 abstract class 中可以有自己的数据成员,也可以 有非 abstarct 的成员方法。

Chapter 7 Interfaces 12/44 Abstract Class -Example Shape CircleRectangle Shape is an abstract class.

13 The Shape Abstract Class Is the following statement valid? –Shape s = new Shape(); No. It is illegal because the Shape class is an abstract class, which cannot be instantiated ( 实例化 ) to create its objects. public abstract class Shape { //method declaration public abstract double area(); public void move() { // non-abstract method // implementation }

14 The Shape Abstract Class public Rectangle extends Shape { protected double w, h; public Rectangle() { w = 0.0; h=0.0; } public double area() { return w * h; } } public Circle extends Shape { protected double r; protected static final double PI = ; public Circle() { r = 1.0; ) public double area() { return PI * r * r; } … }

15 Abstract Class Properties A class with one or more abstract methods is automatically abstract and it cannot be instantiated ( 实例化 ); A class declared abstract, even with no abstract methods can not be instantiated; A subclass of an abstract class can be instantiated if it overrides( 重载 ) all abstract methods by implementation( 实现 ) them; A subclass that does not implement all of the superclass abstract methods is itself abstract; and it cannot be instantiated ( 实例化 ).

16 Example: //Animal.java public abstract class Animal { public String name; public int age; public void print() { System.out.println(" 名字 :"+name); System.out.println(" 大小 :"+age); } public abstract void run(); public abstract void cry(); }

17 class Test { public static void main(String[]args) { Dog a =new Dog(); a.name=" 欢欢 "; a.age=2; a.print(); a.cry(); System.out.println (" 这是一只 "+a.type); } Example: class Dog extends Animal { String type; public Dog() { type=" 宠物狗 "; } public void run(){} public void cry() { System.out.println (" 汪汪叫 "); }

Chapter 7 Interfaces 18/44 Interface interface is a conceptual entity similar to an abstract class. interface 表示的是 "like-a" 关系。 contain only constants ( 常量, final variables) and abstract method (no implementation) - Different from Abstract classes. 接口中定义的变量默认是 public static final 型,且必须初 始化,所以实现类中不能重新定义,也不能改变其值。 接口中的方法默认都是 public , abstract 类型的。

Chapter 7 Interfaces 19/44 Interface( 接口 ) A Java interface is a collection of constants and abstract methods.  abstract method: a method header without a method body; we declare an abstract method using the modifier abstract;  since all methods in an interface are abstract, the abstract modifier is usually left off( 省略 ).

20 // 格式 : InterfaceName.java interface InterfaceName { //Constant/Final Variable Declaration //Methods Declaration–only method header } Printable.java // 接口示例: Printable.java interface Printable { final int MAX=100; void add(); float sum(float x,float y); }

Chapter 7 Interfaces 21/44 Interfaces A class can implement( 实现 ) any number of interfaces, but cannot extend more than one class at a time. Therefore, interfaces are considered as an informal way to implement multiple inheritance in Java.

Chapter 7 Interfaces 22/44 Interfaces A class formally implements an interface by  Using keyword in the class header in the implements clause;  a class can implement multiple interfaces: the interfaces are listed in the implements clause, separated by commas( 逗号 ). If a class asserts( 声称 ) that it implements( 实现 ) an interface, it must define all methods in the interface or the compiler will produce errors.

23 Defining Interface class ClassName implements InterfaceName [, InterfaceName2, …] { // Body of Class } interface InterfaceName { //Constant/Final Variable Declaration //Methods Declaration – only method header }

Chapter 7 Interfaces 24/44 Interface - Example speak() Politician > Speaker speak() Lecturer speak()

25 class Lecturer implements Speaker { public void speak() { System.out.println(“Talks Object Oriented Design and Programming!”); } interface Speaker { public void speak( ) ; } class Politician implements Speaker { public void speak() { System.out.println( “ Talk politics ” ); } Speaker.java Politician.java Lecturer.java

Chapter 7 Interfaces 26/44 Interface Inheritance( 接口的继承 ) Like classes, interfaces can also be extended( 扩展, 继承 ); Interfaces, unlike classes, can extend more than one other interface; The new sub-interface will inherit all the members of the superinterface similar to classes ’ inheritence. This is achieved by using the keyword extends as follows: interface InterfaceName2 extends InterfaceName1,… { // Body of InterfaceName2 }

27 Interface Inheritance — Example 1 interface L extends J, K { boolean l1(); } class I implements L { public int j1() { return 4; } public double k1() { return 6.8; } public boolean l1() { return true; } interface J { int j = 200; int j1(); } interface K { double k1(); }

28 class InterfaceInheritance { public static void main(String args[ ]) { I i = new I(); System.out.println(i.j); System.out.println(i.j1()); System.out.println(i.k1()); System.out.println(i.l1()); } Result : true

29 Interface Inheritance — Example 2 Class A implements two interfaces: I1 & I2. But these two interfaces have the same variable and method. In class A, the method of these two interfaces is implemented only once. For variables with the same name, we need to type-case( 强制数据类型转换 ) them when accessing them; otherwise, compile error will occur if a variable with the same name is used.

30 interface I1{ int nValue = 1; void amethod(); } public class A implements I1, I2 { public void amethod() { System.out.println("Hello word!"); } public static void main(String[] args) { A q = new A(); q.amethod(); //Compile Error //System.out.println(q.nValue); System.out.println(((I1) q).nValue); System.out.println(((I2) q).nValue); } Hello world! 1 2 interface I2 { int nValue = 2; void amethod(); } 执行結果 :

31 Interface Inheritance — Example 2 interface Base { int base = 0; int ambiguous = 10; } interface Set1 extends Base { int set1 = 1; int ambiguous = 100; } interface Set2 extends Base { int set2 = 2; int ambiguous = 1000; }

32 //Example 1: 接口继承 class AmbiguousVariable implements Set1 { public static void main(String args[]) { AmbiguousVariable z = new AmbiguousVariable(); System.out.println(z.base);//0 System.out.println(z.set1);//1 System.out.println(z.ambiguous);//100 System.out.println(((Base)z).ambiguous);//10 System.out.println(((Set1)z).ambiguous);//100 }

33 class AmbiguousVariable implements Set1, Set2 { public static void main(String args[]) { AmbiguousVariable z = new AmbiguousVariable(); System.out.println(z.base);//0 System.out.println(z.set1);//1 //System.out.println(z.ambiguous); // 编译错误 System.out.println( ((Base)z).ambiguous);//10 System.out.println( ((Set1)z).ambiguous);//100 System.out.println( ((Set2)z).ambiguous);//200 }

34 Inheritance and Interface Implementation A general form of interface implementation: This shows a class can extended another class while implementing one or more interfaces. It appears like a multiple inheritance (if we consider interfaces as special kind of classes with certain restrictions or special features). class ClassName extends SuperClass implements InterfaceName […, InterfaceName2, …] { // Body of Class }

Chapter 7 Interfaces 35/44 Example Abstract class:Shape CircleRectangle Interface:2D-Shape Abstract class:Shape CircleRectangle Interface:2D-Shape Abstract class:Shape CircleRectangle Interface:2D-Shape Abstract class:Shape CircleRectangle Interface:2D-Shape Abstract class:Shape CircleRectangle Interface:2D-Shape Abstract class:Shape CircleRectangle Interface:2D-Shape implements extends

36 class Circle extends Shape implements Shape 2D { double radius; String color; public Circle(double r) { redius = r; } public void setColor(String str) { color = str; System.out.println( “ color is “ +color); } public double area() { return (pi*radius*radius); } public abstract class Shape { public abstract void setColor(String str); } interface Shape 2D { final double pi = 3.14; public abstract double area(); }

Chapter 7 Interfaces 37/44 Reference data types( 引用数据类型 ) The reference( 引用 ) types are 1. class( 类 ) types 2. interface ( 接口 ) types 3. array( 数组 ) types  Variables of these types can refer to objects( 指向对 象 ) of the corresponding type.

38 public class ShapeTest { public static void main(String[] args) { Shape2D s;// 声明接口的引用数据 s = new Circle(1.0); // 通过实现接口的类创建接口的 引用数据 s.area();// 只能调用接口中定义的方法 //s.setColor( “ red ” );// 否则,就是语法错误 }

39 综合实例 : 如何声明和使用 以接口和抽象类 为类型的数据 (P39-42) 。 public abstract class Person { String name; int age; {// 初始化块 name ="Parent"; age = -1; } public void initiate(String name, int age){ this.name = name; this.age = age; } public void display() { System.out.println(name+","+a ge); } abstract void detailInfo(); } interface Speaker { public void speak( ); } Speaker.java Person.java

40 Politician.java public class Politition extends Person implements Speaker { public Politition(String name, int age){ super.initiate(name, age); } public void speak(){ System.out.println("Talk politics"); } void detailInfo(){ System.out.println("Polition Implementation"); }

41 Lecturer.java public class Lecturer extends Person implements Speaker { public void speak(){ System.out.println("Talks OO Design and Programming!"); } void detailInfo(){ System.out.println("Lecturer Implementation"); }

42 test.java public class Test { public static void main(String[ ] args) { Speaker speaker; Person person; Politition p = new Politition("Child", 60); p.display(); Lecturer l = new Lecturer(); l.display(); speaker = p; speaker.speak(); speaker = l; speaker.speak(); person = p; person.detailInfo(); person = l; person.detailInfo(); }

43 Java 类实现了一个接口,继承了一个类,如何访问接口与父 类中相同的变量 : public class Test extends Aclass implements Ainterface { public void method() { System.out.println("***Test***"); } public static void main(String[] args) { Test t = new Test(); System.out.println("Aclass's num:"+ ((Aclass)t).num); System.out.println("Ainterface's num:"+ ((Ainterface)t).num); t.method(); ((Ainterface)t).method(); } interface Ainterface { int num=1; void method(); } class Aclass { int num=10; }

Chapter 7 Interfaces 44/44 对象类型转换 Java 中,两个具有继承关系的对象之间可以转换,具体限制:  两个转换的对象之间应该具有继承关系,也就是说只是在子类和父类 的对象之间进行转换,而不是任意两个类。 一个子类对象的类型可以向上转换成它的父类类型,这个转 换过程是安全的。因为父类所具有的信息,子类一般全有。 当然,转换过程中会丢失属于子类而不属于父类的信息。

45 对象类型转换 设 A 类是 B 类的父类,用子类 B 创建一个对象,并 把这个对象的引用放到父类的对象中时,如: 称父类对象 a 是子类对象 b 的上转型对象。 对象的上转型对象的实体是子类负责创建的,但上 转型对象会失去原对象的一些属性和功能。 A a; a=new B(); // 上转型对象 A a; B b=new B(); a=b; // 上转型对象

46 上转型对象的特点: 上转型对象不能操作子类新增的成员变量和方法 上转型对象可以操作子类继承或重写的变量和方法 如果子类重写了父类的某个方法,对象上转型对象调用 时一定是调用这个重写的方法 对象的上转型对象 子类对象新增的变量和方法 继承或重写的变量和方法

47 例: 水果 苹果桔子 红富士苹果

48 注意: 父类创建的对象和子类对象的上转型对象不是一个含义 可以将对象的上转型对象再强制转换到一个子类对象,这时该 子类对象又具备该子类所有的属性和功能。 不可以将父类创建的对象的引用赋值给子类声明的对象。 如果需要把一个父类对象转换成子类类型,则需要强制转换。 如: (这样会导致异常) A a=new A(); B b=(B)a; 类型测试运算符 instanceof 表达式格式: 对象引用 instanceof 类名或接口名

49 例(上转型对象、 instanceof 运算符): class Person { static int age; //age=0 public String detailInfo() { return (" 一个人 "); } class Student extends Person { String info=detailInfo()+"of"+super.detailInfo(); public String detailInfo() { return " 一个学生 "; }

50 测试: Person.java & Student.java public class Extend { public static void main(String[] args){ Student stu1=new Student(); System.out.println(stu1 instanceof Person); //true Person p1=new Person(); System.out.println(p1 instanceof Student); //false p1=stu1; // 上转 System.out.println(stu1.age); //0 System.out.println(stu1.info); // 一个学生 of 一个人 System.out.println(p1.age); //0 //System.out.println(p1.info); // 错误 System.out.println(p1.detailInfo());// 一个学生 System.out.println(((Person)p1).detailInfo());// 一个学生 System.out.println(p1 instanceof Student);//true System.out.println(p1 instanceof Person);//true } 运行结果: true false 0 一个学生 of 一个人 0 一个学生 true 此语句编译 时报错:无 法解析 p1.info ,非 法访问