首页 > 其他 > 详细

instance与可变参数合用,多态性

时间:2014-12-07 06:29:35      阅读:235      评论:0      收藏:0      [点我收藏+]

public class Doubt {

    public static void main(String[] args) {

        Dog d1=new Dog();

        Dog d2=new Zangao();

        Dog d3=new Hasiq();

        Master m=new Master();

        m.feed(d1,d2,d3);

 

 

 

    }

}

class Master {

    void feed(Dog ... d1) {

        for(Dog d:d1) {

            d.eat();

            if(d instanceof Zangao) {

                Zangao z=(Zangao)d;

                z.fight();

                System.out.println("是藏獒");

            }

            if(d instanceof Hasiq) {

                Hasiq h=(Hasiq)d;

                h.play();

                System.out.println("是哈士奇");

            }

        }

    }

}

class Dog {

    void eat() {

        System.out.println("狗吃东西");

 

    }

}

class Zangao extends Dog{

    void eat() {

        System.out.println("藏獒吃东西");

    }

    void fight() {

        System.out.println("我是藏獒,我爱战斗");

    }

}

class Hasiq extends Dog{

    void eat() {

        System.out.println("哈士奇吃东西");

    }

    void play() {

        System.out.println("我是哈士奇,我爱玩耍");

    }

}

instance与可变参数合用,多态性

原文:http://www.cnblogs.com/u0mo5/p/4149089.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!