码迷,mamicode.com
首页 > 编程语言 > 详细

Java 多态

时间:2018-12-29 23:09:18      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:tin   using   port   net   设计   on()   put   oms   import   

在面向对象的程序设计语言中,多态是继数据抽象和继承之后的第三种形态

多态通过分离做什么和怎么做,从另一个角度将接口和实现分离开来,多态不但能改善代码的组织结构和可读性,还可以创建可扩展的程序

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
*///:~

 

Java 多态

标签:tin   using   port   net   设计   on()   put   oms   import   

原文地址:https://www.cnblogs.com/jiangfeilong/p/10197858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!