手记

java入门第二季抽象类测试

Shape.java
  public abstract class Shape {
      abstract void girth();
      abstract  void area();   
  }
Rectangle.java
  import java.util.Scanner;

  public class Rectangle extends  Shape {
     int chang=0;
     int kuan=0;
  void girth()//周长 {
     Scanner input=new Scanner(System.in);
     chang=input.nextInt();
     kuan=input.nextInt();
     System.out.println("矩形的周长是:"+2*(chang+kuan));
}
  void area()//面积 {
     System.out.println("矩形的面积是:"+chang*kuan);
 }
 }
Circle.java

import java.util.Scanner;

   public class Circle extends Shape {
      int r;
      final Double PI=3.14;

   void girth() {
      Scanner input=new Scanner(System.in);
      r=input.nextInt();
      System.out.println("园的周长是"+2*PI*r);
}
   void area() {
      System.out.println("圆的面积是"+PI*r*r);
}
}
Test.java
 public class 测试 {
 public static void main(String[] args) {
     System.out.println("请输入矩形的长和宽");
     Shape rec=new Rectangle();
     rec.girth();
     rec.area();
     System.out.println("请输入圆的半径");
     Shape cir=new Circle();
     cir.girth();
     cir.area();
 }
 }
5人推荐
随时随地看视频
慕课网APP

热门评论

采用Scanner的思路比较好,赞一个

查看全部评论