东北虎国家公园:谁能给我解释一下java的源程序(初学的)

来源:百度文库 编辑:高考问答 时间:2024/05/06 19:49:45
class Animal
{
int height,weight;
Animal ()
{
}
void eat ()
{
System.out.println("Animal eat");
}
void sleep ()
{
System.out.println("Animal sleep");
}
void breathe ()
{
System.out.println("Animal breathe");
}

}

class Fish extends Animal
{
int height;
Fish()
{
}
void breathe()
{
System.out.println("fish breathe");
}
}
class Integration
{
static void fn(Animal an)
{
an.breathe();
}
public static void main (String [] args)
{
Fish fh = new Fish(); //请问,这里是造出来一个fh的东东
Animal an; //而这个是什么.?
an=fh; //而且 fh与an可以赋值?
Integration.fn(an); //这里为什么要用Integration这个类传参数?

}
}

我是新手,请解释一下,谢谢...

而且老师说这是多态的,但是我还是没有明白.

1、fh是类fish的一个实例,有明确指向,被构造函数初始化
2、an是animal类的一个实例没有被初始化。
3、fish是animal的一个子类,基类的引用可以指向子类的引用
4、因为fn这个方法定义在integration类中,并且是一个类方法,static
因此可以通过类名来调用这个函数