/**
定义一个ArrayTools工具类
要求:该类的构造方法被private修饰
提供一个方法,通过该方法能够调用该类的其他方法和成员
*/
class Test
{
public static void main(String[] args)
{
Cat c = new Cat();
AnimalTool.printAnimal(c);
}
}
abstract class Animal
{
private String name;
private int age;
public Animal()
{
}
public 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;
}
public abstract void eat();
public abstract void shout();
}
class Cat extends Animal
{
public Cat()
{
super();
}
public Cat(String name, int age)
{
super(name, age);
}
public void eat()
{
System.out.println("猫吃鱼");
}
public void shout()
{
System.out.println("喵喵。。。");
}
}
class AnimalTool
{
private AnimalTool(){}
public static void printAnimal(Animal a)
{
a.eat();
a.shout();
}
} |
|