多态的意义:
不按照多态的代码:
package day10; public class TestPloy2 { public static void main(String[] args) { Dog1 a0 = new Dog1(); Cat1 a1 = new Cat1(); Monkey1 a2 = new Monkey1(); Tiger a3 = new Tiger(); Sheep a4 = new Sheep(); Cock a5 = new Cock(); a0.eat(); a1.eat(); a2.eat(); a3.eat(); a4.eat(); a5.eat(); } } class Animal1 { public void eat() { } } class Dog1 extends Animal1 { public void eat() { System.out.println("Dog eats bone"); } } class Cat1 extends Animal1 { public void eat() { System.out.println("Cat eats fish"); } } class Monkey1 extends Animal1 { public void eat() { System.out.println("Monkey eats banana"); } } class Tiger extends Animal1 { public void eat() { System.out.println("Tiger eats hunam"); } } class Sheep extends Animal1 { public void eat() { System.out.println("Sheep eats grass"); } } class Cock extends Animal1 { public void eat() { System.out.println("Cock eats rice"); } }
运行截图:
按照多态的思路:
package day10; public class TestPloy2 { public static void main(String[] args) { /* * Dog1 a0 = new Dog1(); Cat1 a1 = new Cat1(); Monkey1 a2 = new Monkey1(); Tiger * a3 = new Tiger(); Sheep a4 = new Sheep(); Cock a5 = new Cock(); a0.eat(); * a1.eat(); a2.eat(); a3.eat(); a4.eat(); a5.eat(); */ // Animal1[] as=new Animal1[] {new Dog1(),new Cat1(),new Tiger(),new Monkey1(),new Sheep(),new Cock()}; Animal1[] as = { new Dog1(), new Cat1(), new Tiger(), new Monkey1(), new Sheep(), new Cock() }; for (int i = 0; i < as.length; i++) { as[i].eat(); } } } class Animal1 { public void eat() { } } class Dog1 extends Animal1 { public void eat() { System.out.println("Dog eats bone"); } } class Cat1 extends Animal1 { public void eat() { System.out.println("Cat eats fish"); } } class Monkey1 extends Animal1 { public void eat() { System.out.println("Monkey eats banana"); } } class Tiger extends Animal1 { public void eat() { System.out.println("Tiger eats hunam"); } } class Sheep extends Animal1 { public void eat() { System.out.println("Sheep eats grass"); } } class Cock extends Animal1 { public void eat() { System.out.println("Cock eats rice"); } }
运行结果:
可以看到,运行结果和之前是一样的。但是代码比之前简洁了。
小结:多态的作用就在于把不同子类的对象,统一看做是父类对象来处理,屏蔽了不同子类的差异,使得代码具有更好的通用性。
buy动物例子-多态:
代码:
package day10; public class TestPloy2 { public static void main(String[] args) { /* * Dog1 a0 = new Dog1(); Cat1 a1 = new Cat1(); Monkey1 a2 = new Monkey1(); Tiger * a3 = new Tiger(); Sheep a4 = new Sheep(); Cock a5 = new Cock(); a0.eat(); * a1.eat(); a2.eat(); a3.eat(); a4.eat(); a5.eat(); */ // Animal1[] as=new Animal1[] {new Dog1(),new Cat1(),new Tiger(),new Monkey1(),new Sheep(),new Cock()}; // Animal1[] as = { new Dog1(), new Cat1(), new Tiger(), new Monkey1(), new Sheep(), new Cock() }; /* * for (int i = 0; i < as.length; i++) { as[i].eat(); } // */ // feed(new Dog1()); // feed(new Cat1()); // feed(new Monkey1()); Animal1 a=buyAnimal(2); System.out.println(a);//返回对象 Animal1 b=buyAnimal(3000); System.out.println(b); } /* * static void feed(Dog1 d) { * * } static void feed(Cat1 c) { * * } ... */ static void feed(Animal1 a) { a.eat();//传什么动物,就调用什么方法 } static Animal1 buyAnimal(int money) { if(money<5) return new Cock(); else if(money<50) return new Cat1(); else if(money<200) return new Dog1(); else if(money<500) return new Sheep(); else if(money<2000) return new Monkey1(); else return new Tiger(); } } class Animal1 { public void eat() { } } class Dog1 extends Animal1 { public void eat() { System.out.println("Dog eats bone"); } } class Cat1 extends Animal1 { public void eat() { System.out.println("Cat eats fish"); } } class Monkey1 extends Animal1 { public void eat() { System.out.println("Monkey eats banana"); } } class Tiger extends Animal1 { public void eat() { System.out.println("Tiger eats hunam"); } } class Sheep extends Animal1 { public void eat() { System.out.println("Sheep eats grass"); } } class Cock extends Animal1 { public void eat() { System.out.println("Cock eats rice"); } }
运行截图:
多态用在参数上 m(A,a):m方法允许A类或A的某个子类对象作为实参。
多态作用在返回值上 A m():m方法返回的是A类或A的某个子类对象。