Darker李
2014-11-09 21:19
package com.lee.poly; /*Shape类*/ public abstract class Shape { public abstract double size(); public abstract double boundry(); }
Rectangle类:
package com.lee.poly; public class Rectangle extends Shape{ public double len; public double hei; public Rectangle(double len, double hei) { super(); this.len = len; this.hei = hei; } public void setLen(double len) { this.len = len; } public void setHei(double hei) { this.hei = hei; } @Override public double size() { double size = len * hei; return size; } @Override public double boundry() { double bou = (len + hei ) * 2; return bou; } }
Circle类:
package com.lee.poly; public class Circle extends Shape{ public double r; public static double pi = 3.14; public Circle(double r) { super(); this.r = r; } public void setR(double r) { this.r = r; } @Override public double size() { double size = pi * r * r; return size; } @Override public double boundry() { double bou = 2 * pi * r; return bou; } }
测试类:
package com.lee.poly; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入长方形的长:"); double len = scan.nextDouble(); System.out.println("请输入长方形的宽:"); double hei = scan.nextDouble(); Rectangle rect = new Rectangle(len, hei); System.out.println("长方形的面积是:" + rect.size()); System.out.println("长方形的周长是:" + rect.boundry()); System.out.println("请输入圆形的半径:"); double r = scan.nextDouble(); Circle cir = new Circle(r); System.out.println("圆形的面积是:" + cir.size()); System.out.println("圆形的周长是:" + cir.boundry()); scan.close(); } }
结果:
请输入长方形的长: 12 请输入长方形的宽: 6 长方形的面积是:72.0 长方形的周长是:36.0 请输入圆形的半径: 6 圆形的面积是:113.03999999999999 圆形的周长是:37.68
成员变量要用private,
是要写程序吗
Java入门第二季 升级版
530652 学习 · 6091 问题
相似问题