实验要求:应用抽象类或接口等面向对象程序设计技术的特点 实验内容:编写求解三角形,矩形,圆的周长和面积
实验思路: 创建类: 1:首先需要创建父类用来存放几何图形的公共属性和方法 2:有几个不同种类的形状就需要创建几个类 3:在父类中定义和构造所有几何形状必须包含的属性(颜色,名字等) 4:对于每个几何形状特有的属性定义和构造在子类中完成
代码如下:
shape类:
package shape; public class shape { String color; public shape() {//子类实例化时,默认调用父类的无参构造方法 } public shape(String color) { this.color=color; } //设置和获取几何形状的基本属性(颜色,面积,周长) public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getArea() {//面积 return 0; } public double setArea() { return 0; } public double getGirth() {//周长 return 0; } public double setGirth() { return 0; } public String getInfo(){//获取函数调用的基本信息 return color; } }
矩形:
package shape; public class Recetagle extends shape{ //定义关于矩形的参数 double width; double length; //子类并不一定非要无参构造方法,直接使用含参会更方便一些 public Recetagle(String color,double width,double length) { this.color=color; this.length=length; this.width=width; } //设置和获取矩形的特有属性 public double getWidth() { return width; } public double getLength() { return length; } public void setLength() { this.length = length; } public void setWidth() { this.width = width; } //继承父类中,所有几何形状都包含的属性 @Override public String getColor() { return super.getColor(); } @Override public double getArea() { return width*length; } @Override public double getGirth() { return width+length; } @Override public String getInfo() { return "颜色为:"+super.getInfo()+",宽为:"+width+",长为:"+length; } }
三角形:
package shape; public class Square extends shape { //定义关于三角形的参数 double length1,length2,length3,higth; public Square(String color,double length1,double length2,double length3,double higth) { this.color=color; this.length1=length1; this.length2=length2; this.length3=length3; this.higth=higth; } @Override public double getGirth() { return length1+length2+length3; } @Override public String getInfo() { return "颜色:"+super.getInfo()+",三边分别为:"+length1+","+length2+","+length3; } @Override public double getArea() { return length3*higth/2; } @Override public String getColor() { return super.getColor(); } public double getLength1() { return length1; } public void setLength1() { this.length1 = length1; } public double getLength2() { return length2; } public void setLength2() { this.length2 = length2; } public double getLength3() { return length3; } public void setLength3() { this.length3 = length3; } }
圆:
package shape; public class Circle extends shape{ //定义圆特有的属性 double radius; public Circle(String color,double radius) { this.color=color; this.radius = radius; } @Override public String getColor() { return super.getColor(); } @Override public double getArea() { return Math.PI*radius*radius; } @Override public double getGirth() { return 2*Math.PI*radius; } @Override public String getInfo() { return "颜色为:"+color+",半径为:"+radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } }
测试类:
package shape; public class Test { public static void main(String[] args) { Circle circle = new Circle("紫色", 10); System.out.println("圆的周长为:" + circle.getGirth()); System.out.println("圆的面积为:" + circle.getArea()); System.out.println("圆的基本信息为:" + circle.getInfo()); Square square = new Square("白色", 12, 11, 9,2); System.out.println("三角形的周长为:" + square.getGirth()); System.out.println("三角形的面积为:" + square.getArea()); System.out.println("三角形的基本信息为:" + square.getInfo()); Recetagle recetagle = new Recetagle("蓝色", 12, 11); System.out.println("矩形的周长为:" + recetagle.getGirth()); System.out.println("矩形的面积为:" + recetagle.getArea()); System.out.println("矩形的基本信息为:" + recetagle.getInfo()); } }
输出:
圆的周长为:62.83185307179586 圆的面积为:314.1592653589793 圆的基本信息为:颜色为:紫色,半径为:10.0 三角形的周长为:32.0 三角形的面积为:9.0 三角形的基本信息为:颜色:白色,三边分别为:12.0,11.0,9.0 矩形的周长为:23.0 矩形的面积为:132.0 矩形的基本信息为:颜色为:蓝色,宽为:12.0,长为:11.0