当前位置: 技术问答>java相关
向各位前辈请教一个关于switch语句的问题,谢谢:)
来源: 互联网 发布时间:2015-08-26
本文导语: 最近在读Thinking in java,遇到一点问题,请大家帮忙:) 书上的源程序如下: class Shape { void draw(){} void erase(){} } class Circle extends Shape { void draw(){System.out.println("Circle.draw()");} void erase(){System.out.println("Circle.eras...
最近在读Thinking in java,遇到一点问题,请大家帮忙:)
书上的源程序如下:
class Shape
{
void draw(){}
void erase(){}
}
class Circle extends Shape
{
void draw(){System.out.println("Circle.draw()");}
void erase(){System.out.println("Circle.erase()");}
}
class Square extends Shape
{
void draw(){System.out.println("Square.draw()");}
void erase(){System.out.println("Square.erase()");}
}
public class Shapes
{
public static Shape randShape()
{
switch((int)(Math.random()*2))
{
default:
case 0: return new Circle();
case 1: return new Square();
}
}
public static void main(String args[])
{
Shape[] s=new Shape[10];
for(int i=0;i
书上的源程序如下:
class Shape
{
void draw(){}
void erase(){}
}
class Circle extends Shape
{
void draw(){System.out.println("Circle.draw()");}
void erase(){System.out.println("Circle.erase()");}
}
class Square extends Shape
{
void draw(){System.out.println("Square.draw()");}
void erase(){System.out.println("Square.erase()");}
}
public class Shapes
{
public static Shape randShape()
{
switch((int)(Math.random()*2))
{
default:
case 0: return new Circle();
case 1: return new Square();
}
}
public static void main(String args[])
{
Shape[] s=new Shape[10];
for(int i=0;i