全文预览

《面向对象程序设计作业答案》.doc

上传者:随心@流浪 |  格式:doc  |  页数:11 |  大小:0KB

文档介绍
The 1st line of f() Originated in f() The 4th line of main() The 5th line of main() 4. 分析下面程序代码的输出结果: class Shape { void draw(){ System.out.println("Shape.draw()"); }} class Circle extends Shape { void draw() { System.out.println("Circle.draw()"); }} class Square extends Shape { void draw() { System.out.println("Square.draw()"); }} class ShapeGenerator { public Shape getShape(int index) { switch(index) { default: case 0: return new Circle(); case 1: return new Square(); }} public void shapeDraw(Shape s){ s.draw(); }} public class Shapes { private static ShapeGenerator gen = new ShapeGenerator(); public static void main(String[] args) { Shape[] s= new Shape[3]; for(int i= 0;i< s.length; i++) s[i] = gen.getShape(i); for(int i= 0;i< s.length; i++) gen.shapeDraw(s[i]); }} 输出结果为: Circle.draw()

收藏

分享

举报
下载此文档