Java实验报告
班级 学号 姓名
完成时间
评分等级
实验四 类的继承
一、实验目的
(1)理解抽象类与接口的使用;
(2)了解包的作用,掌握包的设计方法。
二、实验要求
(1)掌握使用抽象类的方法。
(2)掌握使用系统接口的技术和创建自定义接口的方法。
(3)了解 Java 系统包的结构。
(4)掌握创建自定义包的方法。
三、实验内容
(一)抽象类的使用
- 设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法。 继承该抽象类定义三角型、矩形、圆。 分别创建一个三角形、矩形、圆存对象,将各类图形的面积输出。
(p-a)(p-b)*(p-c)) 其中,a,b,c为三条边,p=(a+b+c)/2
2.编程技巧
(二)使用接口技术
1定义接口Shape,其中包括一个方法size(),设计“直线”、“圆”、类实现Shape接口。分别创建一个“直线”、“圆”对象,将各类图形的大小输出。
2.编程技巧
(1) 接口中定义的方法在实现接口的具体类中要重写实现;
(2) 利用接口类型的变量可引用实现该接口的类创建的对象。
四、实验过程(请自己调整格式)
package Test; import java.util.Scanner; abstract class Shape { public abstract double area();//定义抽象类方法 } class Triangle extends Shape { private double a; private double b; private double c; public Triangle(double a, double b, double c) {//书上给出的定义是通过构造为属性赋值 this.a = a; this.b = b; this.c = c; }//这是后面在测试类中写Triangle triangle = new Triangle(a, b, c);发生错误通过软件自动加上的东西 public double getA() {//数据的封装 return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } public double getC() { return c; } public void setC(double c) { this.c = c; }//数据的封装 public double area() { double p = (this.a + this.b + this.c) / 2; return Math.sqrt(p * (p - this.a) * (p - this.b) * (p - this.c)); } } class Rectangle extends Shape { private double width; private double hight; public Rectangle(double width, double hight) {//同上16-20 this.hight = hight; this.width = width; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getHight() { return hight; } public void setHight(double hight) { this.hight = hight; } public double area() { return this.hight * this.width; } } class Circle extends Shape { private double redius; public Circle(double reidus) {//同上16-20 this.redius = reidus; } public double getRedius() { return redius; } public void setRedius(double redius) { this.redius = redius; } public double area() { return Math.PI * this.redius * this.redius; } } public class Test {//测试类进行对上面方法的测试 public static void main(String args[]) { System.out.println("请输入三角形边长"); Scanner s = new Scanner(System.in);//数据的输入 double a = s.nextDouble(); double b = s.nextDouble(); double c = s.nextDouble(); Triangle triangle = new Triangle(a, b, c);//新建一个对象 if ((a + b) > c && (a + c) > b && (b + c) > a && a > 0 && b > 0 && c > 0) { System.out.println("三角形的面积为:" + triangle.area()); } else { System.out.println("此输入不符合三角形的定义请重启动程序"); return; } Scanner q = new Scanner(System.in); System.out.println("请输入矩形长宽"); double width = q.nextDouble(); double hight = q.nextDouble(); Rectangle rectangle = new Rectangle(width, hight); if (width > 0 && hight > 0) { System.out.println("矩形的面积为:" + rectangle.area()); } else { System.out.println("不符合矩形的定义"); return; } Scanner r = new Scanner(System.in); System.out.println("请输入圆的半径"); double reidus = r.nextDouble(); Circle circle = new Circle(reidus); if (reidus > 0) { System.out.println("圆的面积为" + circle.area()); } else { System.out.println("不符合矩形的定义"); return; } } }
来源:博客园
作者:风靡心落
链接:https://www.cnblogs.com/fengmixinluo/p/11647625.html