第七次学习总结&&第五次实验报告

一、实验目的

(1)理解抽象类与接口的使用;
(2)了解包的作用,掌握包的设计方法。

二、实验要求

(1)掌握使用抽象类的方法。
(2)掌握使用系统接口的技术和创建自定义接口的方法。
(3)了解 Java 系统包的结构。
(4)掌握创建自定义包的方法。

三、实验内容

(一)抽象类的使用
设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法。 继承该抽象类定义三角型、矩形、圆。 分别创建一个三角形、矩形、圆存对象,将各类图形的面积输出。
注:三角形面积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.实验代码

package 抽象类;

abstract class 形状{
public String shape;
public double area;
    public String getShape(){
    return shape;
}
    public void setShape(String shape){
    this.shape=shape;
}
    public double getArea(){
    return area;
}
    public void setArea(double area){
    this.area = area;
}
    public void work(){
    if(this.area()!=0)
        System.out.println(this.shape+"的面积为:"+this.area());
    else
        System.out.println("Wrong");
}
    public abstract double area();
}

class 三角形 extends 形状{
    private double a,b,c;
    public double getA() {
    return a;
}
    public double getB() {
    return b;
}
    public double getC() {
    return c;
}
    public 三角形(String shape,double a,double b,double c){
    this.a=a;
    this.b=b;
    this.c=c;
    this.shape=shape;
}
    public double area(){
    if(a+b>c&&a+c>b&&b+c>a&&a>0&&b>0&&c>0){
        double p=(a+b+c)/2;
        return Math.sqrt((p*(p-a)*(p-b)*(p-c)));
    }
    else
        return 0;
    }
}
class 矩形 extends 形状{
    private double width,high;
    public double getWidth(){
    return width;
}
    public void setWidth(double width){
    this.width = width;
}
    public double getHigh(){
    return high;
}
    public void setHigh(double high){
    this.high = high;
}
    public 矩形(String shape,double width,double high){
    this.high=high;
    this.width=width;
    this.shape=shape;
}
    public double area(){
    if(high>0&&width>0)
        return high*width;
    else
        return 0;
    }
}
class 圆 extends 形状{
    private double banjing;
    public double getBanjing(){
    return banjing;
}
    public void setBanjing(double banjing){
    this.banjing = banjing;
}
    public 圆(String shape,double banjing){
    this.banjing=banjing;
    this.shape=shape;
}
    public double area(){
    if(banjing>0)
        return Math.PI*Math.pow(banjing, 2);
    else
        return 0;
    }
}
    public class 面积{
    public static void main(String[] args){
          形状 s[]= {new 三角形("三角形",3,4,5),new 矩形("矩形",2,3),new 圆("圆",2)};
    for(int i=0;i<3;i++){
        s[i].work();
    }
  }
}

实验结果

正确结果:
数据错误时:

2.实验代码

package 接口;

import java.util.Scanner;
interface shape{
    public void size();
}
class yuan implements shape{
    private double banjing;
    public double getBanjing(){
    return banjing;
}
    public void setBanjing(double banjing){
    this.banjing=banjing;
}
    public yuan(double banjing){
    setBanjing(banjing);
}
    public void size(){
    if(banjing<=0)
        System.out.println("Wrong");
    else{
        System.out.println("圆的面积为:"+ Math.PI*Math.pow(banjing, 2));
        System.out.println("圆的周长为:"+ Math.PI*2*banjing);
        }
    }
}
class line implements shape{
    private double x1,y1,x2,y2;
    public double getX1(){
    return x1;
}
    public void setX1(double x1){
    this.x1 = x1;
}
    public double getY1(){
    return y1;
}
    public void setY1(double y1){
    this.y1 = y1;
}
    public double getX2(){
    return x2;
}
    public void setX2(double x2){
    this.x2 = x2;
}
    public double getY2(){
    return y2;
}
    public void setY2(double y2){
    this.y2 = y2;
}
    public line(double x1,double y1,double x2,double y2){
    this.x1=x1;
    this.x2=x2;
    this.y1=x2;
    this.y2=y2;
}
    public void size(){
    System.out.println("直线的长度为:"+ Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2)));
    }
}
public class 图形输出{
    public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    System.out.println("请输入圆的半径:");
    double banjing=sc.nextDouble();
    yuan p1=new yuan(banjing);
    p1.size();
    System.out.println("请输入直线两点端点的坐标:");
    double x1=sc.nextDouble();
    double y1=sc.nextDouble();
    double x2=sc.nextDouble();
    double y2=sc.nextDouble();
    line p2=new line(x1,y1,x2,y2);
    p2.size();
    }

}

实验结果

正确结果:
数据错误是:

学习总结

(1)instanceof关键字
Java中的一个双目运算符,用来测试一个对象是否为一个类的实例,用法为:boolean result = obj instanceof Class;
注意事项:编译器会检查 obj 是否能转换成右边的class类型,如果不能转换则直接报错,如果不能确定类型,则通过编译。
(2)抽象类与接口的关系

这一点知识是在网上寻找的,上节课为了赶上拿错了数据结构的书,而且坐在太后面看不太清,故上网查询。

原文地址:https://www.cnblogs.com/songzhihaoT1/p/11656155.html