问答详情
源自:10-3 Java 中的抽象类

我的代码如下

package test;


public abstract class Shape {
 public Shape(){
  System.out.println("形状信息是:");
 }
 public abstract void area();
 public abstract void perimeter();
}

package test;


public class Rectangle extends Shape {
    private int ln=0;
    private int width=0;
    public Rectangle (int l, int w) {
     this.width = w;
     this.ln = l;
     System.out.println("此长方形长是:"+this.ln);
     System.out.println("此长方形宽是:"+this.width);
    }
 @Override
 public void area() {
  // TODO Auto-generated method stub
        int ar=ln*width;
        System.out.println("此长方形面积是:"+ar);
 }


 @Override
 public void perimeter() {
  // TODO Auto-generated method stub
        int pe = 2*(ln+width);
        System.out.println("此长方形的周长是:"+pe);
 }


}

package test;


public class Cricle extends Shape {
 int radius;
 double pi=3.14;
 public Cricle (int r){
  this.radius=r;
  System.out.println("此圆形半径是:"+this.radius);
 }


 @Override
 public void area() {
  // TODO Auto-generated method stub
        double ar=pi*radius;
        System.out.println("此圆形面积是:"+ar);
 }


 @Override
 public void perimeter() {
  // TODO Auto-generated method stub
        double pe=2*pi*radius;
        System.out.println("圆形周长是:"+pe);
 }


}

package test;
import java.util.Scanner;


public class Inital {


 public static void main(String[] args) {
  // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
  System.out.println("请输入你要选择的形状:1.圆形 2.长方形");
        int num = input.nextInt();
        if(num==1){
         Scanner input2 = new Scanner(System.in);
         System.out.println("请输入圆形半径");
         int r = input2.nextInt();
         Cricle cricle = new Cricle(r);
         cricle.area();
         cricle.perimeter();
        }
        else{
         Scanner input3 = new Scanner(System.in);
         Scanner input4 = new Scanner(System.in);
         System.out.println("请输入长方形的 长和宽");
         int l = input3.nextInt();
         int w = input4.nextInt();
         Rectangle rectangle = new Rectangle(l,w);
         rectangle.area();
         rectangle.perimeter();
         
         
        }
 }


}


提问者:Ngu_smy 2015-08-09 12:26

个回答

  • 杨宝恒
    2016-02-04 12:52:32

    大神能不能注释一下

    public class Cricle extends Shape {
     int radius;
     double pi=3.14;
     public Cricle (int r){
      this.radius=r;
      System.out.println("此圆形半径是:"+this.radius);
     }

    这一段是什么意思呢?谢谢!

  • 小州州
    2015-08-16 10:20:52

    很好。棒。