shape类(父类)
package com.yby2;
public class shape {
public void draw() {
System.out.println("打印图形为:");
}
}
circle类
package com.yby2;
public class circle extends shape {
@Override
public void draw() {
System.out.println("--------------------");
System.out.println("打印一个圆形:o");
}
}
square类
package com.yby2;
public class square extends shape{
@Override
public void draw() {
System.out.println("--------------------");
System.out.println("打印一个正方形:口");
}
}
Test类
package com.yby2;
import java.util.Random;
public class shapeTest {
public static void main(String[] args) {
circle c = new circle();
c.draw();
square s = new square();
s.draw();
System.out.println("按回车继续!");
System.out.println("将随机打印一个图形:");
for(;;){
int n = new Random().nextInt(2);
switch ( n ){
case 0:random(new circle(),0);break;
case 1:random(new square(),2);break;
}
break;
}
}
public static void random(shape e,int n){
e.draw();
if (n == 0){
if (e instanceof circle){
e.draw();
}
}
else if(n == 1){
if (e instanceof square){
e.draw();
}
}
}
}