实验四 类的继承
实验目的
理解抽象类与接口的使用;
了解包的作用,掌握包的设计方法。
实验要求
掌握使用抽象类的方法。
掌握使用系统接口的技术和创建自定义接口的方法。
了解 Java 系统包的结构。
掌握创建自定义包的方法。
实验内容
(一)抽象类的使用
设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法。 继承该抽象类定义三角型、矩形、圆。 分别创建一个三角形、矩形、圆存对象,将各类图形的面积输出。
注:三角形面积s=sqrt(p(p-a)(p-b)*(p-c)) 其中,a,b,c为三条边,p=(a+b+c)/2
2.编程技巧
(1) 抽象类定义的方法在具体类要实现;
(2) 使用抽象类的引用变量可引用子类的对象;
(3) 通过父类引用子类对象,通过该引用访问对象方法时实际用的是子类的方法。可将所有对象存入到父类定义的数组中。

package Zuoye4;
abstract class shapes {
    public abstract double print();   
}
class Triangle extends shapes { 
    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;              
    }
    public double print() { 
        double p=(a+b+c)/2;
        return  Math.sqrt(p*(p-a)*(p-b)*(p-c));
    }
}
class Rectangle extends shapes {
    private double w;
    private double h; 
    public Rectangle(double w, double h){
        this.h=h;
        this.w=w;    
    }
    public double print() {      
        return w*h;  
    }
}
class Circular extends shapes {
    double r;     
    public Circular(double r){  
        this.r=r;  
    }
    public double print() {     
        return r*r*Math.PI;
    }
}
public class Tuxing{
    public static void main(String[] args){
        shapes Triangle=new Triangle(3,4,5);
        shapes Rectangle=new Rectangle(4,3);
        shapes Circular=new Circular(5);     
        System.out.println("三角形的面积: "+Triangle.print());
        System.out.println("矩形的面积: "+Rectangle.print());
        System.out.println("圆的面积: "+Circular.print());     
    }
}



(二)使用接口技术
1定义接口Shape,其中包括一个方法size(),设计“直线”、“圆”、类实现Shape接口。分别创建一个“直线”、“圆”对象,将各类图形的大小输出。
编程技巧
(1) 接口中定义的方法在实现接口的具体类中要重写实现;
(2) 利用接口类型的变量可引用实现该接口的类创建的对象。

package Zuoye4;
import java.util.Scanner;
interface Shapes{
public double size();
}
class spot implements Shapes{
    private double x1,x2,y1,y2;
    public spot(double x1,double x2,double y1,double y2) {
        this.x1=x1;
        this.x2=x2;
        this.y1=y1;
        this.y2=y2;
    }
    public double size() {
        return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
    }
}
class Circular implements Shapes{
    private double reidus;
    public Circular(double reidus) {
        this.reidus=reidus;
    }
    public double size() {
        return Math.PI*this.reidus*this.reidus;
    }
}
public class Tuxing{
    public static void main(String args[]) {
        Scanner spot=new Scanner(System.in);
        System.out.println("请输入两点的坐标");
        double x1=spot.nextDouble();
        double y1=spot.nextDouble();
        double x2=spot.nextDouble();
        double y2=spot.nextDouble();
        spot line=new spot(x1,y1,x2,y2);
        System.out.println("直线的大小为:"+line.size());
        Scanner c=new Scanner(System.in);
        System.out.println("请输入圆的半径");
        double reidus=c.nextDouble();
        Circular circle=new Circular(reidus);
            System.out.println("圆的大小为:"+circle.size());   
            return;
        }
    }

学习总结
(1)抽象类与接口的应用。
抽象类的实际应用-------模板设计(abstract)
接口的实际应用-------制定标准(interface)
(2)Object类
tostring方法
equals方法
(3)异常
1,数组超出绑定异常:ArrayIndexOutOfBoundsException.
2,数字格式化异常:NumberFormatException.
3,算术异常:ArithmeticException.
try{ //有可能出现异常的语句
}catch(异常类 异常对象){ //编写异常的处理语句
}finally{
一定会运行到的程序代码
}
4.异常类的继承结构
在JAVA中所有捕获范围小的异常必须放在捕获大的异常之前,否则程序在编译时会出现错误提示。
(4)关键字throws
public 返回值类型 方法名称(参数列表.....)throws 异常类{ }