




已阅读5页,还剩24页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
面向对象技术及语言 Object-Oriented Technology & Language,第8章 Interface & Inner Classes,主讲教师: 饶若楠 上海交通大学计算机科学与工程系 ,第8章 Interfaces & Inner Classes,8.1 Interfaces 8.1.1 “multiple inheritance” in Java 8.1.2 Extending an interface with inheritance 8.1.3 Grouping constants 8.1.4 Initializing fields in interfaces 8.1.5 Nesting interfaces 8.2 Inner Classes 8.2.1 Inner classes and upcasting 8.2.2 Inner classes in methods and scopes 8.2.3 Anonymous inner classes 8.2.4 The link to the outer class 8.2.5 Nested classes,第8章 Interfaces & Inner Classes,8.2.6 Referring to the outer class object 8.2.7 Reaching outward from a multiply-nested class 8.2.8 Inheriting frominner classes 8.2.9 Can inner classes be overriden 8.2.10 Local inner classes 8.2.11 Inner class identifiers 8.3 Why inner classes? 8.3.1 Closure & Callbacks 8.3.2 Inner classes & control frameworks,8.1 接口(Interface),所谓接口就是对操作规范的说明。通俗地说,接口只是说明函数应该做什么(What),但没有定义函数如何做(How),Elided/Iconic Representation (“lollipop”),8.1 接口Interface,1. 接口的定义 接口的定义包括接口声明和接口体两部分。如下: public interface InterfaceName extends superInterfaceList /接口声明 type constantName = Value; /常量声明,可为多个 retumType methodName ( paramList ); /方法声明,可为多个 例如:interface Shape public void draw(); 一个接口可以有多个父接口,用逗号隔开,子接口继承父接口中所有的常量和方法。,8.1 接口Interface,2. 接口的实现 在类的声明中用implements子句来表示一个类使用某个接口,在类体中可以使用接口中定义的常量,而且必须实现接口中定义的所有方法。一个类可以实现多个接口,在implements子句中用逗号分隔。 例如:public class Triangle implements Shape public void draw() ,5.9 接口Interface,3. 接口可以作为一种引用类型来使用。任何实现该接口的类的实例都可以存储在该接口类型的变量中,通过这些变量可以访问类所实现的接口中的方法。Java运行时系统动态地确定该使用哪个类中的方法。 例如: Shape abc = new Triangle();,8.1接口Interface - 例子,interface Collection int MAX_NUM=100; void add (Object obj); void delete (Object obj); Object find (Object obj); int currentCount ( ); ,class FIFOQueue implements collection void add ( Object obj ) void delete( Object obj ) Object find( Object obj ) int currentCount ,8.1 接口Interface - 例子,class InterfaceType public static void main( String args ) Collection c = new FIIFOQueue(); add( obj ); ,把接口作为一种数据类型可以不需要了解对象所对应的具体的类,而着重于它的交互界面,“multiple inheritance” in Java (p319 -323),Because an interface has no implmentation at all - that is, there is no storage associated with an interface - theres nothing to prevent may interfaces from being combined.,Example: c08: Adventure.java (p320),Name collision when combining interfaces Example: c08: InterfaceCollision.java (p322),Extending an interface with inheritance (p323 -324),You can add new method declarations to an interface by using inheritance, and you can also combine several interfaces into a new interface with inheritance.,Example: c08: HorrorShow.java (p323),Grouping constants (p324 -327),Because any fields you put into an interface are automatically static and final, the interface is a convenient tool for creating groups of constant.,Example: c08: Months.java (p324) c08: Month.java (p325),Initializing fields in interfaces (p327 -328),Fields defined in interfaces are automatically static and final. Those cannot be “blank finals,” but they can be initialized with nonconstant expressions.,Example: c08: RandVals.java (p327) c08: TestRandvals.java (p327),Nesting interfaces (p328 -331),Interfaces may be nested within classes and within other interfaces.,Example: c08: NestingInterfaces.java (p328),Inner classes (p331-p333),Its possible to place a class definition within another class definition. This is called an inner class.,Example: c08: Parcel1.java (p331),Inner classes and upcasting (p333-p335),Inner classes really come into their own when you start upcasting to a base class, and in particular to an interface.,Example: c08: Destination.java (p333) c08: Contents.java (p334) c08: TestParcel.java (p334),Inner classes in methods and scopes (p335-p338),Inner classes can be created within a method or even an arbitrary scope.,Example: c08: Wrapping.java (p336) c08: Parcel4.java (p336) c08: Parcel5.java (p334),Anonymous inner classes (p338-p342),Example: c08: Parcel6.java (p338) c08: Parcel7.java (p339) c08: Parcel8.java (p339) c08: AnonymousConstructor.java (p340) c08: Parcel9.java (p341),The link to the outer class (p342-p344),When you create an inner class, an object of that inner class has a link to the enclosing object that made it.,Example: c08: sequence.java (p342),Nested classes (p344-p347),static inner classes.,Example: c08: Parcel10.java (p345) c08: IInterface.java (p346) c08: TestBed.java (p346),Referring to the outer class object (p347-p348),Example: c08: Parcel11.java (p347),Reaching outward from a multiply-nested class(p348-p349),Example: c08: MultiNestingAccess.java (p348),Inheriting from inner classes (p349),Example: c08: InheritInner.java (p349),Can inner classes be overriden ?(p350),Example: c08: BigEgg.java (p350),Local inner classes (p352),Example: c08: LocalInnerClass.java (p352),Inner class identifier (p354),LocalInnerClass$2.class,Why inner classes? (p354-357),Example: c08: MultiInterfaces.java (p355) c08: MultiImple
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 期末5年级数学试卷
- 教育资源共建共享报告
- 林杨讲数学试卷
- 洛阳市涧西区数学试卷
- 青岛莱西初二数学试卷
- 2025年智能化配电与电控装置项目合作计划书
- 历年湖北省高考数学试卷
- 培智学校数学试卷
- 辽宁历年高考数学试卷
- 临沂高三期中数学试卷
- Unit 10 单元作业设计 人教版七年级英语下册
- 人造血管内瘘个案护理
- 全国各省(直辖市、自治区)市(自治州、地区)县(县级市)区名称一览表
- 小学数学知识体系构建研究课题
- 《变电运行培训》课件
- 幼儿发展的关键指标与行为观察
- 班主任工作手册(适合中小学)
- 瑜伽生活方式中心生活馆项目建议书
- 吊装作业票(样本)
- 【新课标】2022版小学数学课标之模拟卷6套
- 固体矿产钻探工真题模拟汇编(共634题)
评论
0/150
提交评论