抽象类练习:抽象动物

抽象类:

package Animal_;

public abstract class Animal {
	public abstract void cry();
	public abstract String getAnimalName();
}

子类:

package Animal_;

public class Dog extends Animal{

	@Override
	public void cry() {
		System.out.println("汪汪");
	}

	@Override
	public String getAnimalName() {
		return "Dog";
	}
	
}
package Animal_;

public class Cat extends Animal{

	@Override
	public void cry() {
		System.out.println("喵喵");
	}
	@Override
	public String getAnimalName() {
		// TODO Auto-generated method stub
		return "Cat";
	}
	
}
模拟类:

import Animal_.*;
class Simulator {
	Animal something;
	public void playSound(Animal something){
		this.something=something;
		System.out.println("animal:"+this.something.getAnimalName());
		this.something.cry();
	}
}

主程序:

import Animal_.*;

public class Application {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Simulator x=new Simulator();
		x.playSound(new Cat());
		x.playSound(new Dog());
	}

}


posted on 2017-11-18 10:18  MACHINE_001  阅读(202)  评论(0编辑  收藏  举报

导航