Java继承与多态汇总

实验报告 课程名称 实验名称 学生学号 面向对象程序设计 实验三、继承与多态 姓名 日期 班级 2016-9-30 实验目的: 通过本次实验,要求学生能进一步掌握继承和多态的概念,熟练定义类、派生类、抽象类、抽象方法、虚拟方法和重载方法。 实验条件: 电脑一台、能上网查阅资料。 首先定义基本几何图形的抽象基类(Shape),然后定义Shape的派生类Circle(圆形)、Square(正方形)和Triangle(三角形)。要求通过抽象方法、虚拟方法和重载方法来计算各种图形的面积和周长。(提示:求三角形的面积用海伦公式:S?L*(L?a)*(L?b)*(L?c),其中a,b,c是三角形的边长,L=(a+b+c)/2,即周长的一半。) 源代码: Shape.java package com.lit.jsj.work3; public abstract class Shape { } public abstract double getLength(); public abstract double getArea(); Square.java package com.lit.jsj.work3; import com.lit.jsj.work3.Shape; public class Square extends Shape{ public void setD(double d) { } public Square(double d) { this.d = d; public double getD() { } return d; private double d; } } // TODO Auto-generated constructor stub this.d=d; setD(d); @Override public double getLength() { } @Override public double getArea() { } // TODO Auto-generated method stub return d*d; // TODO Auto-generated method stub return 4*d; Triagle.java package com.lit.jsj.work3; public class Triagle extends Shape{ public double getC() { return c; public void setB(double b) { } this.b = b; public double getB() { } return b; public void setA(double a) { } this.a = a; private double a,b,c; public double getA() { } return a; } } public void setC(double c) { } public Triagle(double a,double b,double c) { } public boolean IsTriangle(){ } @Override public double getLength() { } @Override public double getArea() { } // TODO Auto-generated method stub if (IsTriangle()){ } double l=getLength()/2; double area=Math.sqrt(l*(l-a)*(l-b)*(l-c)); return area; return 0; // TODO Auto-generated method stub if (IsTriangle()){ } else{ } return 0; return a+b+c; return (a+b>c)&&(b+c>a)&&(a+c>b); // TODO Auto-generated constructor stub this.a=a; setB(b); setC(c); this.c = c; }else {

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