java抽象类和抽象方法实验报告 下载本文

内容发布更新时间 : 2024/11/7 9:36:43星期一 下面是文章的全部内容请认真阅读。

实验报告 课程名称 实验名称 学生学号 面向对象程序设计 实验五、抽象类和抽象方法 姓名 日期 班级 2018-4-28 实验目的: 通过本次实验,要求学生能进一步掌握抽象类和抽象方法的概念,熟练定义抽象类和抽象方法。 实验条件: 电脑一台、能上网查阅资料。 首先定义基本几何图形的抽象基类(Shape),然后定义Shape的派生类Circle(圆形)、Square(正方形)和Triangle(三角形)。要求通过抽象方法和方法覆盖来计算各种图形的面积和周长。(提示:求三角形的面积用海伦公式:S?L*(L?a)*(L?b)*(L?c),其中a,b,c是三角形的边长,L=(a+b+c)/2) ,即周长的一半。) 源代码: package Shape; public abstract class Shape { public void setName(String name) { } abstract double area(); abstract double length(); this.name = name; public String getName() { } return name; protected String name; public Shape(){ name=\图形类\; } public Shape (String name){ } super(); this.name=name; } package Shape; public class Circle extends Shape { } private double r; public Circle(double r,String name){ } public double getR() { } public void setR(double r) { } double area() { } double length() { } // TODO Auto-generated method stub return Math.PI*r*2; // TODO Auto-generated method stub return Math.PI*Math.pow(r, 2); this.r = r; return r; super(name); this.r=r; public String toString() { // TODO Auto-generated method stub return name+\周长是:\+length()+\+\面积是:\+area(); package Shape; public class Square extends Shape{ public double getA() { private double a; public Square(double a,String name) { } super(name); this.a=a; } return a; public void setA(double a) { } double area() { } double length() { } // TODO Auto-generated method stub return 4*a; // TODO Auto-generated method stub return Math.pow(a, 2); this.a = a; package Shape; public class Triangle extends Shape{ private double a; private double b; private double c; public Triangle(double a,double b,double c,String name) { } public double getA() { } public void setA(double a) { } public double getB() { } public void setB(double b) { } public double getC() { return c; this.b = b; return b; this.a = a; return a; super(name); this.a=a; this.b=b; this.c=c;