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

实验四 类的继承

实验目的

理解抽象类与接口的使用;
了解包的作用,掌握包的设计方法。
实验要求
掌握使用抽象类的方法。
掌握使用系统接口的技术和创建自定义接口的方法。
了解 Java 系统包的结构。
掌握创建自定义包的方法。

实验内容

(一)抽象类的使用

设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法。 继承该抽象类定义三角型、矩形、圆。 分别创建一个三角形、矩形、圆存对象,将各类图形的面积输出。
注:三角形面积s=sqrt(p(p-a)(p-b)*(p-c)) 其中,a,b,c为三条边,p=(a+b+c)/2
2.编程技巧

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

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

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

1.实验源码
import static java.lang.StrictMath.sqrt;

abstract class shape{
    private String shape;
    private 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 abstract double area();
}
class triangle extends shape{
    private double 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;
    }

    triangle(double a, double b, double c) {
        this.a=a;
        this.b=b;
        this.c=c;
    }
    public double area() {
        double p;
        double s;
        p=(this.a+this.b+this.c)/2;
        s=sqrt(p*(p-a)*(p-b)*(p-c));
        return s;

    }
}   
class rectangle extends shape{  //长:L(l)高:H(h)
    private double l,h;

    public double getH() {
        return h;
    }

    public void setH(double h) {
        this.h = h;
    }

    public double getL() {
        return l;
    }

    public void setL(double l) {
        this.l = l;
    }
    rectangle(double l,double h){
        this.h=h;
        this.l=l;
    }
    @Override
    public double area() {
        double s1;
        s1=this.h*this.l;
        return s1;
    }
}
class circle extends shape{
    private double r;

    public double getR() {
        return r;
    }

    public void setR(double r) {
        this.r = r;
    }
    circle(double r){
        this.r=r;
    }
    @Override
    public double area() {
        double s2;
        s2= Math.PI*r*r;
        return s2;
    }
}
class shape1{
    public static void main(String[] args) {
        shape triangle = new triangle(7,8,9);
        System.out.println("三角形的面积为:"+triangle.area());
        shape rectangle = new rectangle(10,20);
        System.out.println("矩形的面积为:"+rectangle.area());
        shape circle = new circle(5);
        System.out.println("圆形的面积为:"+circle.area());
    }
}
2.实验结果截图

3.实验过程

听过老师上课所讲的这个题目并不难。圆周率:Math.PI

(二)使用接口技术

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

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

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

1.实验源码
interface Shape1{
    public double size();
}
class straight implements Shape1 {
    private double l;
    straight(double l){
        this.l=l;
    }

    @Override
    public double size() {
        return this.l;
    }
}
class circle1 implements Shape1{
    private double r;
    circle1(double r){
        this.r=r;
    }
    @Override
    public double size() {
        double area;
        area=Math.PI*r*r;
        return area;
    }
}
class size1{
    public static void main(String[] args) {
        Shape1 straight = new straight(25);
        System.out.println("直线的长度为:"+straight.size());
        Shape1 circle1 = new circle1(3);
        System.out.println("圆的面积为:"+circle1.size());
    }
}
2.实验结果截图

3.实验过程

跟着书上的198页的代理操作来写的

本周总结

1.学习了抽象类:
(1)抽象类里面可以有普通方法,以及抽象类是不能直接实定化;
(2)抽象类必须被子类继承,子类(如果不是抽象类)必须覆写抽象类中的全部抽象方法
(3)定义格式:

abstract class 抽象类名称{
    属性;
    访问权限  返回值类型  方法名称(参数){
    return 返回值;
    }
    访问权限 abstract 返回值类型 方法名称(参数);
}

2.学习了接口:
(1)接口一般只有方法声明没有定义
(2)接口没有方法体,只能通过一个具体的类去实现其中的方法体
(3)定义格式:

interface 接口名称{
    全局常量;
    抽象方法;
}

二者之间的选择:两种都可以使用的情况下,优先使用接口,因为避免单接口的局限性
特殊:一个抽象类中可以包含多个接口,一个接口中可以包含多个抽象类

关于自己 :看视频的进度稍微提快了点,希望自己可以利用起碎片时间,将视频进度赶上来

原文地址:https://www.cnblogs.com/muxixixixi/p/11654955.html