/**
* 定义一个标识形状的接口
*
*/
public interface Shape {
/**
* 表示形状的方法
* @return
*/
public String desc();
}
public class Circular implements Shape {
@Override
public String desc() {
return "这是一个半径为1的圆形!";
}
}
public class Rectangle implements Shape{
@Override
public String desc() {
return "这是一个长为3宽为2的矩形";
}
}
public class Square implements Shape{
@Override
public String desc() {
return "这是一个边长为2的正方形";
}
}
public class ShapeTest {
Shape shape = null;
public String getShape(int i){
if(i==0){
shape = new Circular();
}else if(i==1){
shape = new Rectangle();
}else if(i==2){
shape = new Square();
}else{
System.out.println("您输入的不满足任何形状!");
}
return shape.desc();
}
public static void main(String[] args) {
ShapeTest test = new ShapeTest();
System.out.println(test.getShape(1));
}
}