第七周课程总结&实验报告(五)

第七周课程总结

1.java异常

try{
        //有可能出现异常的语句
}catch(异常类 异常对象){
       //编写异常的处理对象
}[catch(异常类 异常对象){
       //编写异常的处理对象
}catch(异常类 异常对象){
       //编写异常的处理对象
}......]
[finally{
        一定会运行到的程序代码;
}]

2.throws关键字

实验五 类的继承

一、 实验目的

(1) 理解抽象类与接口的使用;

(2) 了解包的作用,掌握包的设计方法。

二、 实验要求

(1) 掌握使用抽象类的方法。

(2) 掌握使用系统接口的技术和创建自定义接口的方法。

(3) 了解 Java 系统包的结构。

(4) 掌握创建自定义包的方法。

三、 实验内容

(一)抽象类的使用
1. 设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法。 继承该抽象类定义三角型、矩形、圆。 分别创建一个三角形、矩形、圆存对象,将各类图形的面积输出。
注:三角形面积s=sqrt(p(p-a)(p-b)*(p-c)) 其中,a,b,c为三条边,p=(a+b+c)/2
2.编程技巧
(1) 抽象类定义的方法在具体类要实现;
(2) 使用抽象类的引用变量可引用子类的对象;
(3) 通过父类引用子类对象,通过该引用访问对象方法时实际用的是子类的方法。可将所有对象存入到父类定义的数组中。
(二)使用接口技术
1. 定义接口Shape,其中包括一个方法size(),设计“直线”、“圆”、类实现Shape接口。分别创建一个“直线”、“圆”对象,将各类图形的大小输出。
2. 编程技巧
(1) 接口中定义的方法在实现接口的具体类中要重写实现;
(2) 利用接口类型的变量可引用实现该接口的类创建的对象。

四、 实验过程(请自己调整格式)

(一)抽象类的使用

1. 设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法。 继承该抽象类定义三角型、矩形、圆。 分别创建一个三角形、矩形、圆存对象,将各类图形的面积输出。

注:三角形面积s=sqrt(p(p-a)(p-b)*(p-c)) 其中,a,b,c为三条边,p=(a+b+c)/2

2.编程技巧

(1) 抽象类定义的方法在具体类要实现;

(2) 使用抽象类的引用变量可引用子类的对象;

(3) 通过父类引用子类对象,通过该引用访问对象方法时实际用的是子类的方法。可将所有对象存入到父类定义的数组中。

实验源码:

package homework;

abstract class Shape {
	public abstract double getArea();
	
	public static void main(String[] args) {
		Triangle t = new Triangle(3,4,5);
		Rectangle r = new Rectangle(4,5);
		Cricle c = new Cricle(5);
		
		System.out.println("TriangleArea:"+t.getArea());
		System.out.println("RectangleArea:"+r.getArea());
		System.out.println("CricleArea:"+c.getArea());
	}
}

class Triangle extends Shape{          //三角形
	private double length;
	private double width;
	private double height;
	
	Triangle(double length, double width, double height){
		this.length = length;
		this.width = width;
		this.height = height;
	}
	
	public double getArea() {
		double result = (length + width + height)/2;
        return Math.sqrt(result*(result-length)*(result-width)*(result-height));
	}
}

class Rectangle extends Shape{          //矩形
	private double length;
	private double width;
	
	Rectangle(double length, double width){
		this.length = length;
		this.width = width;
	}
	
	public double getArea() {
		return length*width;
	}
}

class Cricle extends Shape{          //圆形
	private double radius;
	
	Cricle(double radius){
		this.radius = radius;
	}
	
	public double getArea() {
		return Math.PI*Math.pow(radius,2);
	}
}

实验中遇到的问题:在最开始看题目的时候有点没思路,不知道怎样将继承子类放入抽象类中(果然放了个国庆假==重读)

解决办法:助教热心帮我解答了,中间加上子类的属性和构造方法还有主函数调用

实验结果:

(二)使用接口技术

1.定义接口Shape,其中包括一个方法size(),设计“直线”、“圆”、类实现Shape接口。分别创建一个“直线”、“圆”对象,将各类图形的大小输出。

2.编程技巧

(1) 接口中定义的方法在实现接口的具体类中要重写实现;

(2) 利用接口类型的变量可引用实现该接口的类创建的对象。

实验源码:

package homework;

public class Some {
	public static void main(String[] args) {
		Line l = new Line(3);
		Cricle1 c = new Cricle1(5);	
		System.out.println("LineLength:");
		System.out.println("CricleArea:");
		l.size();
		c.size();
	}
}

interface Shape1 {
	double size();
}

class Line implements Shape1{
	private double l;
	public Line(double l) {
		this.l = l;
	}
	
	public double size() {
		return l;
	}
}

class Cricle1 implements Shape1{
	private double c;
	public Cricle1(double c) {
		this.c = c;
	}
	
	public double size() {
		return Math.PI*Math.pow(c,2);
	}
}

实验中遇到的问题:

实验结果:

四、 结论

1.在写题的过程中发现第一题可以有三种不同的输出方式

①普通方法

	public static void main(String[] args) {
		Triangle t = new Triangle(3,4,5);
		Rectangle r = new Rectangle(4,5);
		Cricle c = new Cricle(5);
		
		System.out.println("TriangleArea:"+t.getArea());
		System.out.println("RectangleArea:"+r.getArea());
		System.out.println("CricleArea:"+c.getArea());
	}

②百度上看到的:构造对象时创建数组,再将内容直接输出

Shape s[] = new Shape[3];           创建三个类
        s[0] = new Triangle(4,5,6);
        s[1] = new Rectangle(4,5);
        s[2] = new Circle1(47);
        for (int i=0;i<=2;i++)                 利用for循环依次输出它们的面积
        {
             System.out.println(s[i].getArea());
        }

③从助教处学到的简便方法:不创建对象直接用new输出

public class Main {

    public static void main(String[] args) {
        System.out.println(new triangle(3, 4, 5).Area());
        System.out.println(new rectangle(1, 1).Area());
        System.out.println(new circle(1).Area());
    }
}
原文地址:https://www.cnblogs.com/LILI6/p/11635233.html