首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java 温习笔记——多态性

2012-11-19 
java 复习笔记——多态性1)通过覆盖父类的方法来实现,在运行时根据传递的对象引用,来调用相应的方法。2)多态

java 复习笔记——多态性
1)
通过覆盖父类的方法来实现,在运行时根据传递的对象引用,来调用相应的方法。

2)
多态须具备:

1. 基类 和各个子类

2. 基类 引用, 指向实例化的子类对象.

3)

class Animal
{
int height,weight;
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;
void breathe()
{
System.out.println("fish bubble");
}
}

class Integration
{
static void fn(Animal an)
{
an.breathe();
}
public static void main(String[] args)
{
Fish fh=new Fish();
Animal an;
an=fh;
Integration.fn(an);//或an.breathe();
       }
}

热点排行