您的当前位置:首页正文

Java继承与多态详解

2024-10-18 来源:威能网
实验报告 课程名称 实验名称 学生学号 面向对象程序设计 实验三、继承与多态 姓名 日期 班级 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 { Circle.java package com.lit.jsj.work3; import com.lit.jsj.work3.Shape; public class Circle extends Shape { } @Override public double getArea() { } // TODO Auto-generated method stub return Math.PI*Math.pow(r, 2); public Circle(double r){ } @Override public double getLength() { } // TODO Auto-generated method stub return 2*Math.PI*r; this.r=r; public void setR(double r) { } this.r = r; private double r; public double getR() { } return r; FirgureMain.java package com.lit.jsj.work3; import java.util.Scanner; public class FirgureMain { public static void main(String[] args) { System.out.println(\"请输入一个圆的半径r:\"); Scanner reader=new Scanner(System.in); double r=reader.nextDouble(); Shape shape; shape=new Circle(r); System.out.println(\"圆的周长和面积分别是:\"+shape.getLength()+\面积:\"+shape.getArea()); System.out.println(\"请输入三角形的三条边:\"); double a=reader.nextDouble(); double b=reader.nextDouble(); double c=reader.nextDouble(); Triagle triagle=new Triagle(a, b, c); boolean flag=triagle.IsTriangle(); if (!flag){ } shape=triagle; System.out.println(\"三条边构不成三角形\"); return; System.out.println(\"三角形的周长和面积分别是:\"+shape.getLength()+\面积:\"+shape.getArea()); System.out.println(\"请输入一个正方形的边长:\"); double d=reader.nextDouble(); Square square=new Square(d); System.out.println(\"正方形的周长为:\"+square.getLength()+\面积:\"+square.getArea()); } } 实验结果: 实验总结(结论或问题分析): 实验成绩

任课教师签名

因篇幅问题不能全部显示,请点此查看更多更全内容