JAVA多态示例

时间:2014-05-26 17:53:47   收藏:0   阅读:295

这多态,我觉得是最利害的。在开发大型程序中。

但,也是需要经过足够多的实践经验才能随心利用的。

bubuko.com,布布扣
class Quadrangle{
    private Quadrangle[] qtest = new Quadrangle[6];
    private int nextIndex = 0;
    public void draw(Quadrangle q){
        if(nextIndex < qtest.length){
            qtest[nextIndex] = q;
            System.out.println(nextIndex);
            nextIndex++;
        }
    }
    public static void main(String[] args){
        Quadrangle q = new Quadrangle();
        q.draw(new Square());
        q.draw(new Parallelogram());
    }
}
class Square extends Quadrangle{
    public Square(){
    System.out.println("this is a Square class.");
    }
}
class Parallelogram extends Quadrangle{
    public Parallelogram(){
    System.out.println("this is a Parallelogram class.");
    }
}
bubuko.com,布布扣

this is a Square class.
0
this is a Parallelogram class.
1

JAVA多态示例,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!