第6章 程序设计基础Java程序设计.pptVIP

  • 1
  • 0
  • 约1.31万字
  • 约 49页
  • 2016-06-19 发布于江西
  • 举报
第6章 程序设计基础Java程序设计

public class Test { static void Print(Shape sh) //使用抽象类Shape作为参数 { sh.print();} static double Area(Shape sh) {return sh.area();} public static void main(String[] args) { Shape sh[]; //使用抽象类定义数组 sh=new Shape[2];//创建两个抽象类Shape引用变量 sh[0]=new Circle(10); //第0个数组元素引用到圆对象 sh[1]=new Cylinder(20,10);//第1个数组元素引用到圆柱体对象 Print(sh[0]); System.out.println(面积是:+Area(sh[0])); Print(sh[1]); System.out.println(的表面积是:+Area(sh[1])); } } 6.3 抽象类及抽象方法 6.3 抽象类及抽象方法 例6-5在主类Test中使用了抽象类数组引用变量,其中第0个元素引用到Circle对象,第1个元素引用到了Cylinder对象,这在Java中是允许的,即允许父类引用

文档评论(0)

1亿VIP精品文档

相关文档