package case2;
public class extendsTest {
public static void main(String args[]) {
// 实例化一个Cat对象,设置属性name和age,调用voice()和eat()方法,再打印出名字和年龄信息
/********* begin *********/
Cat c = new Cat("大花猫",6);
c.voice("大花猫");
c.eat("大花猫");
System.out.println(c.getName()+c.getAge()+"岁");
/********* end *********/
// 实例化一个Dog对象,设置属性name和age,调用voice()和eat()方法,再打印出名字和年龄信息
/********* begin *********/
Dog d = new Dog ("大黑狗" , 8);
d.voice("大黑狗");
d.eat("大黑狗");
System.out.println(d.getName()+d.getAge()+"岁");
/********* end *********/
}
}
class Animal {
/********* begin *********/
private String name;
private int age;
Animal(String name , int age)
{
this.name = name;
this.age = age ;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge ()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
/********* end *********/
}
class Cat extends Animal {
// 定义Cat类的voice()和eat()方法
/********* begin *********/
Cat(String name , int age)
{
super(name,age);
}
void voice(String name)
{
System.out.println(name+"喵喵叫");
}
void eat(String name)
{
System.out.println(name+"吃鱼");
}
/********* end *********/
}
class Dog extends Animal {
// 定义Dog类的voice()和eat()方法
/********* begin *********/
Dog(String name , int age)
{
super(name , age);
}
void voice (String name)
{
System.out.println(name+"汪汪叫");
}
void eat (String name)
{
System.out.println(name+"吃骨头");
}
/********* end *********/
}