题目描述
编写抽象类Animal,
包含构造方法
抽象方法
包含成员变量:name, age
Main类及Dog类已经编写好。
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner r = new Scanner(System.in);
String name;
int age;
name = r.next();
if( !r.hasNextInt() )
return;
age = r.nextInt();
if( age < 0 )
return;
Animal dog=new Dog(name,age);
dog.eat();
}
}
class Dog extends Animal
{
public Dog(String name, int age)
{
super(name,age);
}
public void eat()
{
System.out.println("My name is "+ name);
System.out.println("I am "+ age +" years old");
System.out.println("I like eating bone");
}
}
输入
宠物名字,年龄
输出
详见样例
样例输入
wangcai 2
样例输出
My name is wangcai I am 2 years old I like eating bone
代码
abstract class Animal{
protected String name;
protected int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
abstract public void eat();
}