1 图形
package org.jsoft.shape;
public abstract class Drawing {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int x, y ;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public abstract void Drawing() ;
}
2 正方形
package org.jsoft.shape;
public class Squre extends Drawing{
@Override
public void Drawing() {
// TODO Auto-generated method stub
System.out.println("这个图形是:"+getName()+"他的连长是"+getX());
}
}
3 矩形
package org.jsoft.shape;
public class Rectangle extends Drawing{
@Override
public void Drawing() {
// TODO Auto-generated method stub
System.out.println("这个图形是:"+getName()+"他的宽是"+getX()+"他的高是:"+getY());
}
4 圆形
package org.jsoft.shape;
public class Circle extends Drawing{
private int r;
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
@Override
public void Drawing() {
// TODO Auto-generated method stub
System.out.print("这个图形是:"+getName()+"他的半径是"+getR());
}
}
5 函数
package org.jsoft.shape;
public class Function {
public Drawing getShape(int i){
Drawing r=new Rectangle();
Drawing s=new Squre();
Drawing c=new Circle();
Circle cc=(Circle)c;
if(i==0){
cc.setName("圆形");
cc.setR(1);
return cc;
}
else if(i==1){
r.setName("矩形");
r.setX(2);
r.setY(3);
return r;
}
else if(i==2){
s.setX(2);
s.setY(2);
s.setName("圆形");
return s;
}else {
return null;
}
}
}
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
//System.out.println("请输入函数i的值 ");
int i = sc.nextInt();
Function f = new Function();
if (i >= 3 || i < 0) {
System.out.print("你输入的有误,请重新输入");
}
else if (i==0) {
f.getShape(0).Drawing();
} else if (i == 1){
f.getShape(1).Drawing();
}
else if(i==2){
f.getShape(2).Drawing();
}
}
}
}