• java-多态2


    多态的意义:

    不按照多态的代码:

    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的某个子类对象。

  • 相关阅读:
    writeToFile 读写文件问题
    iOS 设置代理过程
    iOS UIView 快速修改 frame,
    touches,motion触摸事件响应
    集合视图控制器(CollectionViewController) 、 标签控制器(TabBarController) 、 高级控件介绍
    触控(Touch) 、 布局(Layout)
    Autoresizing和AutoLayout
    动画(Animation) 、 高级动画(Core Animation)
    ios开发环境 分支语句 、 循环结构(for) 、 循环结构
    oc内存管理总结(一)
  • 原文地址:https://www.cnblogs.com/SpringChuXin/p/13782589.html
Copyright © 2020-2023  润新知