java吧 关注:1,300,349贴子:12,842,890
  • 4回复贴,共1

【我知道这样的求助的确很烦】各位大神解救下菜鸟吧——

取消只看楼主收藏回复



1楼2012-12-19 21:39回复
    JAVA课没好好听,书上没例题看不懂,貌似接口这东西很弱智啊……但我怎么就是弄不出来啊!!
    interface Exchange{
    public double area();
    public void girth();
    }
    abstract class Shape{
    abstract protected double area();
    abstract protected void girth();
    }
    class Rectangle extends Shape implements Exchange{
    float width,length;
    Rectangle(float w,float l){
    width=w;
    length=l;
    }
    public double area(){
    return width*length;
    }
    public void girth(){
    double g=(width+length)*2;
    System.out.println("and the girth is:"+g);
    }
    }
    class Circle extends Shape implements Exchange{
    float radius;
    Circle(float r){
    radius=r;
    }
    public double area(){
    return radius*radius*3.14;
    }
    public void girth(){
    double g=radius*2*3.14;
    System.out.println("and the girth is:"+g);
    }
    }
    public class Shape_ex_2 { /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Exchange rc=new Rectangle(6,12);
    Exchange cir=new Circle(4);
    System.out.println("Rectangle's area is:"+rc.area());
    rc.girth();
    System.out.println("Circle's area is:"+cir.area());
    cir.girth();
    } }


    2楼2012-12-19 21:40
    回复
      2026-03-26 19:20:47
      广告
      不感兴趣
      开通SVIP免广告
      题目是把下面这个改成用接口的
      abstract class Shape{
      abstract protected double area();
      abstract protected void girth();
      }
      class Rectangle extends Shape{
      float width,length;
      Rectangle(float w,float l){
      width=w;
      length=l;
      }
      public double area(){
      return width*length;
      }
      public void girth(){
      double g=(width+length)*2;
      System.out.println("and the girth is:"+g);
      }
      }
      class Circle extends Shape{
      float radius;
      Circle(float r){
      radius=r;
      }
      public double area(){
      return radius*radius*3.14;
      }
      public void girth(){
      double g=radius*2*3.14;
      System.out.println("and the girth is:"+g);
      }
      }
      public class Shape_ex { /**
      * @param args
      */
      public static void main(String[] args) {
      // TODO Auto-generated method stub
      Rectangle rc=new Rectangle(6,12);
      Circle cir=new Circle(4);
      System.out.println("Rectangle's area is:"+rc.area());
      rc.girth();
      System.out.println("Circle's area is:"+cir.area());
      cir.girth();
      } }


      3楼2012-12-19 21:50
      回复
        被吞了……………………
        题目是把下面这个改成用接口的
        abstract class Shape{
        abstract protected double area();
        abstract protected void girth();
        }
        class Rectangle extends Shape{
        float width,length;
        Rectangle(float w,float l){
        width=w;
        length=l;
        }
        public double area(){
        return width*length;
        }
        public void girth(){
        double g=(width+length)*2;
        System.out.println("and the girth is:"+g);
        }
        }
        class Circle extends Shape{
        float radius;
        Circle(float r){
        radius=r;
        }
        public double area(){
        return radius*radius*3.14;
        }
        public void girth(){
        double g=radius*2*3.14;
        System.out.println("and the girth is:"+g);
        }
        }
        public class Shape_ex { /**
        * @param args
        */
        public static void main(String[] args) {
        // TODO Auto-generated method stub
        Rectangle rc=new Rectangle(6,12);
        Circle cir=new Circle(4);
        System.out.println("Rectangle's area is:"+rc.area());
        rc.girth();
        System.out.println("Circle's area is:"+cir.area());
        cir.girth();
        } }


        4楼2012-12-19 21:51
        回复
          以下是一个建立矩形和圆,并输出它们相应面积和周长的程序。
          改写成用接口来实现类之间的连接,做一些小改动,但程序功能不变——
          abstract class Shape{
          abstract protected double area();
          abstract protected void girth();
          }
          class Rectangle extends Shape{ //矩形类的构建方法
          float width,length;
          Rectangle(float w,float l){
          width=w; //矩形的宽
          length=l; //矩形的长
          }
          public double area(){
          return width*length; //运算出矩形的面积
          }
          public void girth(){
          double g=(width+length)*2;
          System.out.println("and the girth is:"+g); //运算并输出矩形的周长
          }
          }
          class Circle extends Shape{ //圆类的构建方法
          float radius;
          Circle(float r){
          radius=r; //圆的半径
          }
          public double area(){
          return radius*radius*3.14; //运算出圆的面积
          }
          public void girth(){
          double g=radius*2*3.14; //运算出圆的周长
          System.out.println("and the girth is:"+g);
          }
          }
          public class Shape_ex { /**
          * @param args
          */
          public static void main(String[] args) {
          // TODO Auto-generated method stub
          Rectangle rc=new Rectangle(6,12); //建立新的矩形,宽为6,长为12
          Circle cir=new Circle(4); //建立新的圆,半径是4
          System.out.println("Rectangle's area is:"+rc.area()); //输出矩形面积
          rc.girth();
          System.out.println("Circle's area is:"+cir.area()); //输出圆面积
          cir.girth();
          } }


          7楼2012-12-21 13:38
          回复