java课堂作业(七)

2、

  1 package com.zhuoyue.java0702;
  2 
  3 class Shape{
  4     private double zhouChang;
  5     private double mianJi;
  6     
  7     public double jiSuanZhouChang(){
  8         return zhouChang;
  9     }
 10     
 11     public double jiSuanMianJi(){
 12         return mianJi;
 13     }
 14 }
 15 
 16 class Rect extends Shape{
 17     private double l;
 18     private double w;
 19     private double zhouChang;
 20     private double mianJi;
 21     
 22     public Rect(){
 23         l = 0.0;
 24         w = 0.0;
 25     }
 26     
 27     public Rect(double l,double w){
 28         this.l = l;
 29         this.w = w;
 30     
 31     }
 32     
 33     //重写父类方法
 34     public double jiSuanZhouChang(){
 35         zhouChang = 2 * (l+w);
 36         return zhouChang;
 37     }
 38     
 39     public double jiSuanMianJi(){
 40         mianJi = l*w;
 41         return mianJi;
 42     }
 43     
 44     
 45 }
 46 
 47 class Square extends Rect{
 48     private double b;
 49     private double zhouChang;
 50     private double mianJi;
 51     
 52     public Square(){
 53         b = 0.0;
 54     }
 55     
 56     public Square(double b){
 57         this.b = b;
 58     }
 59     
 60     //重写父类方法
 61     public double jiSuanZhouChang(){
 62         zhouChang = 4 * b;
 63         return zhouChang;
 64     }
 65     
 66     public double jiSuanMianJi(){
 67         mianJi = b * b;
 68         return mianJi;
 69     }
 70 }
 71 
 72 class Circle extends Shape{
 73     private double r;
 74     private double zhouChang;
 75     private double mianJi;
 76     
 77     public Circle(){
 78         
 79     }
 80     
 81     public Circle(double r){
 82         this.r = r;
 83     }
 84     
 85     //重写父类方法
 86     public double jiSuanZhouChang(){
 87         zhouChang = 2 * 3.14 * r;
 88         return zhouChang;
 89     }
 90     
 91     public double jiSuanMianJi(){
 92         mianJi = 3.14 * r * r;
 93         return mianJi;
 94     }
 95 }
 96 
 97 
 98 public class Test {
 99 
100     /**
101      * @param args
102      */
103     public static void main(String[] args) {
104         // TODO Auto-generated method stub
105         Shape shape[] = new Shape[3];
106         shape[0] = new Rect(3,5);
107         System.out.println("矩形的周长是:"+shape[0].jiSuanZhouChang()+"面积是:"+shape[0].jiSuanMianJi());
108         shape[1] = new Square(4);
109         System.out.println("正方形的周长是:"+shape[1].jiSuanZhouChang()+"面积是:"+shape[1].jiSuanMianJi());
110         shape[2] = new Circle(6);
111         System.out.println("圆形的周长是:"+shape[2].jiSuanZhouChang()+"面积是:"+shape[2].jiSuanMianJi());
112     }
113 
114 }

 

该休息了,暂时不写了。。。

posted @ 2013-08-30 13:41  董文博  阅读(243)  评论(0编辑  收藏  举报