java_1111夏丽昀

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();
   }
  
   
  }
  }
}

转载于:https://my.oschina.net/u/3715015/blog/1572334

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值