简单的java实验,涉及到 类继承以及接口问题,方法体的重写(区别于重载)

时间:2025-01-18 22:37:26
  package  test ;
abstract class Animal
{
abstract void cry();
abstract String getAnimalName();
}
class dogs extends Animal
{ @Override
void cry() {
// TODO Auto-generated method stub
System.out.println("小狗"+" 旺旺 ");
} @Override
String getAnimalName() {
// TODO Auto-generated method stub
return "大黄";
} }
class cats extends Animal
{ @Override
void cry() {
// TODO Auto-generated method stub
System.out.println("小猫"+"喵喵 ");
} @Override
String getAnimalName() {
// TODO Auto-generated method stub
return "小红";
} }
class Simulator
{
void display(Animal myAnimal)
{
myAnimal.cry();
System.out.println(myAnimal.getAnimalName());
}
}
public class hello
{
public static void main(String [] args)
{
//dogs mydog = new dogs();
//cats mycat = new cats();
Simulator mySimulator = new Simulator();
mySimulator.display(new dogs());
mySimulator.display(new cats());
} }