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

本届最后思考题中我的代码

package com.form;


//Shape图形类

public abstract class Shape {

double length;

double width;

double radius;

public abstract void perimeter();

public abstract void area();


}

package com.form;

//Rectangle子类


public class Rectangle extends Shape {

@Override

public void perimeter() {

// TODO Auto-generated method stub

System.out.println("长方形的周长为:"+(length+width)*2);

}

@Override

public void area() {

// TODO Auto-generated method stub

System.out.println("长方形的面积为:"+length*width);


}


}


package com.form;


//circle子类

public class Circle extends Shape {

@Override

public void perimeter() {

// TODO Auto-generated method stub

System.out.println("圆的周长为:"+2*Math.PI*radius);

}


@Override

public void area() {

// TODO Auto-generated method stub

System.out.println("圆的面积为:"+Math.PI*radius*radius);

}


}


package com.form;

import java.util.Scanner;


public class Instant {


/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner put=new Scanner(System.in);

Shape rectangle=new Rectangle();

System.out.print("请输入长方形的长:");

rectangle.length=put.nextDouble();

System.out.print("请输入长方形的宽:");

rectangle.width=put.nextDouble();

rectangle.perimeter();

rectangle.area();

Shape circle=new Circle();

System.out.print("请输入圆的半径:");

circle.radius=put.nextDouble();

circle.perimeter();

circle.area();

}


}


提问者:琼子 2015-05-04 15:56

个回答

  • lorenzt
    2015-05-15 14:28:07

    我觉得你应该把以下这几个变量分别定义在不同的类中会好一点,而不应该定义在父类,因为这些都是他们独有的变量

    double length;

    double width;

    double radius;