BloveSaga

在希腊帕尔纳斯山南坡上,有一个驰名世界的戴尔波伊神托所,在它的入口处的巨石上赫然锈刻着这样几个大字: 认识你自己!

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  34 随笔 :: 12 文章 :: 122 评论 :: 0 Trackbacks

 通过覆盖父类的方法来实现,在运行时根据传递对象的引用,来调用相应的方法.
code:
class Animal
{
 int height,weight;
 Animal()
 {
  System.out.println("Animal construct");
 }
 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
{
 Fish()
 {
  System.out.println("Fish construct");
 }
 void breathe()  //override method breathe()
 {
  System.out.println("Fish bubble");
 }
}
class DoMain
{
 static void fn(Animal an)
 {
  an.breathe();
 }
 public static void main(String[] args)
 {
  //Animal an=new Animal();
  Fish fh=new Fish();
  Animal an;
  an=fh;
  DoMain.fn(an);
 }
}

posted on 2006-06-04 11:40 蓝色Saga 阅读(144) 评论(0)  编辑  收藏 所属分类: Basic Study for JAVA

只有注册用户登录后才能发表评论。


网站导航: