Java 多态
在面向对象的程序设计语言中,多态是继数据抽象和继承之后的第三种形态
多态通过分离做什么和怎么做,从另一个角度将接口和实现分离开来,多态不但能改善代码的组织结构和可读性,还可以创建可扩展的程序
java中所有方法都是通过动态绑定实现多态的,我们可以编写只与基类打交道的程序代码
//: reusing/CADSystem.java // Ensuring proper cleanup 确保合适的清理 package object; import java.util.Random; import static net.mindview.util.Print.*; class Shape{ //形状 public void draw(){} //基类只提供公共接口 public void erase(){ } } class Circle extends Shape{ public void draw(){print("Circle draw");} public void erase(){ print("Circle erase");} } class Triangle extends Shape{ public void draw(){print("Triangle draw");} public void erase(){ print("Triangle erase");} } class Square extends Shape{ public void draw(){print("Square draw");} public void erase(){ print("Square erase");} } class RandomShapeGenerator{ private Random rand = new Random(47); public Shape next() { switch(rand.nextInt(3)) { default: case 0: return new Circle(); case 1: return new Square(); case 2: return new Triangle(); } } } public class Shapes{ private static RandomShapeGenerator gen = new RandomShapeGenerator(); public static void main(String[] args) { Shape[] s = new Shape[9]; //fill up the array with shapes for(int i = 0; i< s.length; i++) { s[i] = gen.next();//由对象决定调用哪个方法 } //make polymorphic method calls for(Shape shp : s) shp.draw(); } }/* output: Triangle draw Triangle draw Square draw Triangle draw Square draw Triangle draw Square draw Triangle draw Circle draw *///:~
package object; import static net.mindview.util.Print.*; enum Note{ MIDDLE_C,MIDDLE_D,MIDDLE_E } class Instrument{ void play(Note n){System.out.println("Instrument.play()" + n);}; String what(){ return "Instrument";} void adjust() {print("ADjusting instrument");} } class Wind extends Instrument{ void play(Note n){System.out.println("Wind.play()" + n);}; String what(){ return "Wind";} void adjust() {print("ADjusting Wind");} } class Percussion extends Instrument{ void play(Note n){System.out.println("Percussion.play()" + n);}; String what(){ return "Percussion";} void adjust() {print("ADjusting Percussion");} } class Stringed extends Instrument{ void play(Note n){System.out.println("Stringed.play()" + n);}; String what(){ return "Stringed";} void adjust() {print("ADjusting Stringed");} } class Brass extends Wind{ void play(Note n){System.out.println(" Brass.play()" + n);}; void adjust() {print("ADjusting Brass");} } class Woodwind extends Wind{ void play(Note n){System.out.println(" Woodwind.play()" + n);}; void adjust() {print("ADjusting Woodwind");} } public class Music{ //Does't care abou type,so new types //added to the system still work right public static void tune(Instrument i) { //.. i.play(Note.MIDDLE_C); } public static void tuneAll(Instrument[] e) { for(Instrument i : e) tune(i); } public static void main(String[] args) { //upcasting during additin to the array Instrument[] orchestra = { new Wind(), new Percussion(), new Stringed(), new Brass(), new Woodwind(), }; tuneAll(orchestra); } }/* output: Wind.play()MIDDLE_C Percussion.play()MIDDLE_C Stringed.play()MIDDLE_C Brass.play()MIDDLE_C Woodwind.play()MIDDLE_C *///:~