1、1,Java面向对象程序设计,第5章 接口与多态,2,本章主要内容,final修饰符在方法重写中的作用 abstract修饰符在抽象方法与抽象类的应用 理解并使用接口 适配器设计模式 多态的应用向上转型与可扩展性,3,final修饰符,作用一:常量属性 public static final PI = 3.14; 作用二:继承时,可用来修饰父类方法,防止子类同名方法发生重写。,public class Mammal public final void call( ) System.out.println(“哺乳动物会叫。“); ,public class Cat extend Mammal p
2、ublic void call() /编译时发生错误super.call();System.out.println(“猫喵喵叫“); ,4,方法一:空实现 public void Draw() 方法二:抽象方法 public abstract void Draw();,方法的抽象,演示抽象方法与抽象类,5,abstract 修饰符,由abstract关键字修饰的方法称为抽象方法,由abstract关键字修饰的类称为抽象类,抽象方法必须声明在抽象类中。 抽象方法语法: abstract type method_name(parameter_list); 声明抽象类语法: abstract cla
3、ss 应用场合:父类中的某些抽象方法不包含任何逻辑,子类可以通过方法重写的机制提供这种抽象方法的实现细节。,6,抽象类与抽象方法,抽象方法不具有任何实现代码。 构造方法 和 static 方法不能是抽象的。 抽象类不能被实例化。 抽象类可以具有指向子类对象的对象引用。 抽象类中也可包含非抽象成员方法和成员属性。,演示抽象方法与抽象类,7,接口,当所有方法均为抽象的,即没有具体实现时,可使用interface关键字定义为接口,它是某个事物对外提供的一些功能的申明 。 接口中的方法均为抽象声明的,及没有具体实现;接口中的属性均为静态常量。 使用implements关键字来实现接口,可以利用接口实现
4、多态。,演示接口,8,接口,接口中的方法不能有任何方法体实现。 接口中可以包含属性,但会被隐式地声明为public 、static和final的,存储在该接口的静态存储区域内,而不属于该接口。 接口中的方法可以被声明为public或不声明,但结果都会按照public类型处理。 接口可以继承接口(使用关键字extends)。在继承时,父接口传递给子接口的只是方法说明,而不是具体实现。 一个接口可以有一个以上的父接口,一个类可以在继承父类的同时实现多个接口,即允许多重继承。,9,应用接口的设计单继承接口,10,应用接口的设计多重继承接口,11,应用接口的设计继承类,实现接口,12,接口与抽象类的区
5、别,抽象类可以提供部分已经实现的方法,而接口所有的方法都是抽象的。 抽象类作为公共的父类,为子类的扩展提供基础,包括属性上和行为上的扩展。而接口不重视属性,只重视方法,使得子类可以自由的填补或者扩展接口所定义的方法。 一个抽象类的实现只能由这个抽象类的子类给出,也就是说,这个实现以继承为基础的。而由于类的单继承性,抽象类作为类型定义工具的效能受到局限。接口具有多重继承的优势,使得任何一个实现接口的类,都可看作是这个接口的类型,而一个类可以实现任意多个接口,从而这个类就可以看作多种类型。,13,适配器模式,public interface Target void f1( );void f2( )
6、;void f3( );void f4( );void f5( ); ,public class DefaultAdapter implements Target public void f1( ) public void f2( ) public void f3( ) public void f4( ) public void f5( ) ,public class MyInteresting extends DefaultAdapter public void f5( ) System.out.println(“我就对f5( )方法感兴趣!“); ,public class Test pu
7、blic static void main(String args) MyInteresting obj = new MyInteresting( );Obj.f5( ); ,14,向上转型接口应用,public interface Shape public void erase(); public class Circle implements Shape public void erase() System.out.println(“Circle erase“); public class Line implements Shape public void erase() System.o
8、ut.println(“Line erase“); ,public class Eraser void cleanBoard(Shape s) s.erase(); ,public class Test public static void main(String args) Eraser earser = new Eraser(); Line l = new Line();Circle c = new Circle(); earser.cleanBoard(l); earser.cleanBoard(c); ,15,可扩展性,public interface Shape public voi
9、d erase(); public class Circle implements Shape public void erase() System.out.println(“Circle erase“); public class Line implements Shape public void erase() System.out.println(“Line erase“); public class Triangle implements Shape public void erase( ) System.out.println(“Triangle erase“); ,public c
10、lass Eraser void cleanBoard(Shape s) s.erase(); ,public class Test public static void main(String args) Eraser earser = new Eraser(); Line l = new Line();Circle c = new Circle(); Triangle t = new Triangle();earser.cleanBoard(l); earser.cleanBoard(c); earser.cleanBoard(t); ,16,多态应用实例,有一家公司生产了很多种交通工具,
11、他们希望编写一个软件能够计算各类交通工具的时速; 现在的要求是:每种交通工具时速的计算都是三个参数; 希望在以后增加新的交通工具的时候,不用更改原来的任何代码,以便提高维护性;,17,public interface ToolSpeed public double speed(double a, double b, double c); public class Car007 implements ToolSpeed public double speed(double a, double b, double c) return a * b / c; public class ComputeT
12、ime public static void main(String args) ToolSpeed speed = (ToolSpeed)Class.forName(args0).newInstance();double a = Double.parseDouble(args1);double b = Double.parseDouble(args2);double c = Double.parseDouble(args3);System.out.println(speed.speed(a,b,c); ,public class Plane implements ToolSpeed public double speed(double a, double b, double c) return a + b + c; ,18,本节小结,final修饰符在方法重写中的作用 abstract修饰符在抽象方法与抽象类的应用 理解并使用接口 适配器设计模式 多态的应用向上转型与可扩展性,