java类的继承与多态实验报告

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

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4