A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© wx_d9b6mRbI 中级黑马   /  2015-7-29 22:24  /  1293 人查看  /  21 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
        多态的好处:
                为了提高代码的扩展性和可维护性。
*/
class Animal
{
        public void show()
        {
                System.out.println("show");
        }

        public void eat()
        {
                System. out.println("eat");
        }
}

class Cat extends Animal
{
        public void show()
        {
                System.out.println("show cat");
        }

        public void eat()
        {
                System. out.println("eat 老鼠");
        }
}

class Dog extends Animal
{
        public void show()
        {
                System.out.println("show dog");
        }

        public void eat()
        {
                System. out.println("eat 骨头");
        }
}

class Pig extends Animal
{
        public void show()
        {
                System.out.println("show pig");
        }

        public void eat()
        {
                System. out.println("eat 饲料");
        }
}

class AnimalTool
{
        private AnimalTool(){}

        /*
        public static void printDog(Dog d)
        {
                d.show();
                d.eat();
        }

        public static void printCat(Cat c)
        {
                c.show();
                c.eat();
        }

        public static void printPig(Pig p)
        {
                p.show();
                p.eat();
        }
        */
       
        public static void printAnimal(Animal a)
        {
                a.show();
                a.eat();
        }
}

class DuoTaiDemo5
{
        public static void main(String[] args)
        {
                //一开始,养了一只狗
                Dog d = new Dog();
                d.show();
                d.eat();
                //接着又养了一只狗
                Dog d2 = new Dog();
                d2.show();
                d2.eat();
                //继续养狗
                Dog d3 = new Dog();
                d3.show();
                d3.eat();
                System.out.println("-----------------------");
                //...养了很多只狗,我们就会发现一个问题,养的越多,代码的重复度越高。不好。
                //如何改进呢?思考,他们调用的功能,仅仅是由于对象不一样。所以,我们能不能把
                //每一个调用的整体封装一下呢,然后传递一个变化的对象即可呢?
                //改进版代码
                Dog d4 = new Dog();
                Dog d5 = new Dog();
                Dog d6 = new Dog();
                /*
                printDog(d4);
                printDog(d5);
                printDog(d6);
                */

                //使用工具类
                /*
                AnimalTool.printDog(d4);
                AnimalTool.printDog(d5);
                AnimalTool.printDog(d6);
                */

                AnimalTool.printAnimal(d4);
                AnimalTool.printAnimal(d5);
                AnimalTool.printAnimal(d6);

                System.out.println("-----------------------");
                //接着,改爱好了,喜欢猫了,那就开始养猫
                //基本养猫动作和基本养狗动作一样。
                //直接写改进版
                Cat c = new Cat();
                Cat c2 = new Cat();
                Cat c3 = new Cat();
                /*
                printCat(c);
                printCat(c2);
                printCat(c3);
                */
                //使用工具类
                /*
                AnimalTool.printCat(c);
                AnimalTool.printCat(c2);
                AnimalTool.printCat(c3);
                */
                AnimalTool.printAnimal(c);
                AnimalTool.printAnimal(c2);
                AnimalTool.printAnimal(c3);
                System.out.println("-----------------------");
                //后来,发展了,我喜欢宠物猪。
                //怎么办?
                //我要创建猪对象,并使用
                Pig p = new Pig();
                Pig p2 = new Pig();
                Pig p3 = new Pig();
                /*
                printPig(p);
                printPig(p2);
                printPig(p3);
                */
                //使用工具类
                /*
                AnimalTool.printPig(p);
                AnimalTool.printPig(p2);
                AnimalTool.printPig(p3);
                */
                AnimalTool.printAnimal(p);
                AnimalTool.printAnimal(p2);
                AnimalTool.printAnimal(p3);
                System.out.println("-----------------------");
                //到目前为止,有不好的地方。
                //比如说:测试类中应该只能有main方法。是不能有其他方法的。
                //为了保证不报错,我就得写一下方法。
                //重新定义一个工具类,实现输出效果。
                //宠物猪过时了,开始改养宠物狼。请问,要实现一样的效果,怎么办?

        }

        /*
        public static void printDog(Dog d)
        {
                d.show();
                d.eat();
        }

        public static void printCat(Cat c)
        {
                c.show();
                c.eat();
        }

        public static void printPig(Pig p)
        {
                p.show();
                p.eat();
        }
        */
}

21 个回复

正序浏览
学到了,谢谢分享
回复 使用道具 举报
Mr.Cai 中级黑马 2015-11-27 22:58:32
20#
我还没学到多态,学习了
回复 使用道具 举报
这么多 看晕了  不过写的很详细
回复 使用道具 举报
父类不能访问子类的特有功能
父类通过向下转型才可以访问子类特有方法
回复 使用道具 举报
666                    
回复 使用道具 举报
感谢分享.................
回复 使用道具 举报
感谢分享.................
回复 使用道具 举报
yubail 中级黑马 2015-11-27 19:34:33
14#
谢谢分享
回复 使用道具 举报
Weidan 中级黑马 2015-11-27 19:13:26
13#
//到目前为止,有不好的地方。
//比如说:测试类中应该只能有main方法。是不能有其他方法的。
//为了保证不报错,我就得写一下方法。
//重新定义一个工具类,实现输出效果。
//宠物猪过时了,开始改养宠物狼。请问,要实现一样的效果,怎么办?

  1. public class AnimalTools {
  2.         public static void printAnimal(Animal animal){
  3.                 animal.eat();
  4.                 animal.show();
  5.         }
  6. }
复制代码


这样写吧 可以解决 只要是继承Animal的 就不用给每一个类编写一个工具了
回复 使用道具 举报
思路很清晰,点赞!
回复 使用道具 举报
虽然我不知道你们说的是什么,但看起来好像很厉害的样子
回复 使用道具 举报
加油。。。。。。。。。。。。
回复 使用道具 举报
写的很有水准
回复 使用道具 举报
写得很不错
回复 使用道具 举报
很认真学习...值得我们学习态度
回复 使用道具 举报
逆光奔跑 发表于 2015-7-29 23:14
大神啊,要奋斗学习啊

静瞎拍马屁,小屁孩不学点正经东西:@
回复 使用道具 举报
又看了下代码,发现这个代码不是你敲的,没人会这么敲,好吧,那肯定是水经验的,再帮你顶一贴,只能帮你到这了:P
回复 使用道具 举报
妹子,写的很棒,思路清晰,格式漂亮,但是,很想问下妹子,发论坛是让小弟们学习参考的,可不可下次发代码的时候多动几下纤纤玉手,把注释加上,这才 Perfect! 看好你哦,加油!
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马