计算几何图形的面积
作者:互联网
Shape:
Shape:
package Area; /** * 求面積 * @author Administrator * */ public abstract class Shape { public String getName(){ return this.getClass().getName();//獲得類名稱 } public abstract double getArea(); }View Code
Circle:
package Area; public class Circle extends Shape{ private double radius; public Circle(){ } public Circle(double radius){ this.radius=radius; } public double getArea(){//主要是抽象的實現 return Math.PI*Math.pow(radius,2); } }View Code
Triangle:
package Area; public class Triangle extends Shape{ private double x; private double y; public Triangle(){ } public double getArea(){ return (x*y)/2; } public Triangle(double x,double y){ this.x=x; this.y=y; } }View Code
Test:
package Area; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Circle circle = new Circle(1); System.out.println("the shape is:" + circle.getName()); System.out.println("the Area: "+circle.getArea()); Shape tri=new Triangle(1,1); System.out.println("the shape is:" +tri.getName()); System.out.println("the shape is:" + tri.getArea()); } }View Code
标签:几何图形,Area,double,面积,Shape,radius,计算,Circle,public 来源: https://www.cnblogs.com/helloworld2019/p/10626745.html