面向对象技术及语言-第8章.ppt
面向对象技术及语言 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 interfaces8.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 identifiers8.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:MultiImplementation.java(p356),Closures&Callbacks(p357-360),Example C08:Callbacks.java(p358),Inner classes&control frameworks(p360-367),Example C08:Controller:Event.java(p361)c08:Controller:Controller.java(p362)c08:GreenhouseControls.java(p363-p366),