源自:10-3 Java 中的抽象类
我的代码如下
package com.imooc;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入圆的半径:");
double radius = input.nextDouble();
System.out.println("请输入矩形的长:");
double ll = input.nextDouble();
System.out.println("请输入矩形的宽:");
double ww = input.nextDouble();
Shape sha = new Rectangle(ll,ww);
sha.Perimeter();
sha.area();
Shape sha1 = new Circle(radius);
sha1.Perimeter();
sha1.area();
}
}
提问者:有强迫症的罗小贱
2015-08-07 09:20
个回答
-
package com.imooc;
public abstract class Shape {
public abstract void Perimeter();
public abstract void area();
}
-
package com.imooc;
public class Rectangle extends Shape {
public double l;
public double w;
public Rectangle(double length2,double wide2){
l = length2;
w = wide2;
if(l < 0){
System.out.println("你输入的长度有误,请重新输入");
}
if(w < 0){
System.out.println("你输入的宽度有误吗,请重新输入");
}
}
public void Perimeter(){
double a = 2*(l + w);
System.out.println("矩形的周长="+a);
}
public void area() {
// TODO Auto-generated method stub
double b = l * w;
System.out.println("矩形的面积="+b);
}
}
-
package com.imooc;
public class Circle extends Shape {
double radius;
public Circle(double r){
radius = r;
if (radius < 0) {
System.out.println("你输入的半径有误。请重新输入");
}
}
public void Perimeter(){
//double e = 3.14;
double a = 2*radius*3.14;
System.out.println("圆的周长="+a);
}
public void area() {
// TODO Auto-generated method stub
double b = 3.14 *radius*radius;
System.out.println("圆的面积="+b);
}
}