当前位置: 代码迷 >> Java相关 >> 大家帮我解释个程序,谢谢了
  详细解决方案

大家帮我解释个程序,谢谢了

热度:125   发布时间:2010-07-19 21:30:49.0
大家帮我解释个程序,谢谢了
abstract class Shape{
    abstract void draw();
    abstract void erase();
    }
class Circle extends Shape{
    void draw(){
        System.out.println("I'm a circle");
        }
    void erase(){
        System.out.println("I'm circle s");
        }
    }
class Square extends Shape{
    void draw(){
        System.out.println("I'm a square");
        }
    void erase(){
        System.out.println("I'm square s");
        }
    }
class Triangle extends Shape{
    void draw(){
        System.out.println("I'm a Triangle");
        }
    void erase(){
        System.out.println("I'm Triangle s");
        }
    }
class huxiang{
    public static Shape randShape(){      //能帮我解释下这个方法吗?
        switch((int)(Math.random()*3)){
            default:System.out.println("hello");
            case 0:return new Circle();
            case 1:return new Square();
            case 2:return new Triangle();
            
            }
        }
     public static void main(String args[]){
        Shape[] s=new Shape[9];
        for(int i=0;i<s.length;i++){
            s[i]=randShape();
            s[i].draw();
            s[i].erase();
            }
        }
    }
搜索更多相关的解决方案: 解释  

----------------解决方案--------------------------------------------------------
我已经把关键部分打了注释,可以看看.有不明白的继续提问.
package hh;

/**
* 一个抽象类表示形状,下面是三个形状的具体实现类,分别是圆,正方形,三角形
* 这是一个多态学习的典型例子
* @author baifenghan
*
*/
abstract class Shape {
    abstract void draw();

    abstract void erase();
}

class Circle extends Shape {
    void draw() {
        System.out.println("I'm a circle");
    }

    void erase() {
        System.out.println("I'm circle s");
    }
}

class Square extends Shape {
    void draw() {
        System.out.println("I'm a square");
    }

    void erase() {
        System.out.println("I'm square s");
    }
}

class Triangle extends Shape {
    void draw() {
        System.out.println("I'm a Triangle");
    }

    void erase() {
        System.out.println("I'm Triangle s");
    }
}

class huxiang1 {
    /**
     * 根据下面的解释,实际上这个方法就是随机生成一个形状的实例.
     * @return
     */
    public static Shape randShape() {
       //Math.random()会产生一个[0.0, 1.0)之间的输,那么乘3就会产生一个[0.0 ,3.0) 之间的一个double值
        //强制转换成int类型,在Java中采取数学中的去尾法,即就是只取得整数部分,那么(int) (Math.random() * 3)的
        //范围就是0, 1, 2
        switch ((int) (Math.random() * 3)) {
        default:
            System.out.println("hello");
        case 0:
            return new Circle();
        case 1:
            return new Square();
        case 2:
            return new Triangle();

        }
    }

    public static void main(String args[]) {
        Shape[] s = new Shape[9];
        for (int i = 0; i < s.length; i++) {
            s[i] = randShape();
            s[i].draw();
            s[i].erase();
        }
    }
}

----------------解决方案--------------------------------------------------------
解释的非常清楚了,谢谢。
----------------解决方案--------------------------------------------------------
  相关解决方案