实验四 类的继承

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

实验内容
(一)抽象类的使用

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

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

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

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

1.代码

package test;

import java.util.Scanner;
abstract class Shape {
    public abstract double getArea();
}

class Triangle extends Shape{
    private double a;
    private double b;
    private double c;
    
    public Triangle(double a,double b,double c){
        this.setLenth1(a);
        this.setLenth2(b);
        this.setLenth3(c);
    }
    
     private void setLenth3( double a) {
        // TODO 自动生成的方法存根
        this.a=a;
    }
     public double getLenth1() {
            return a;
        }
    private void setLenth2( double b) {
        // TODO 自动生成的方法存根
        this.b=b;
    }
     public double getLenth2() {
            return b;
        }

    private void setLenth1( double c) {
        // TODO 自动生成的方法存根
        this.c=c;
    }
     public double getLenth3() {
            return c;
        }

    public double getArea() {
            return Math.sqrt((a+b+c)/2*((a+b+c)/2-a)*((a+b+c)/2-b)*((a+b+c)/2-c));
        }
}

class Rectangle extends Shape{
    double h;
    double l;
    public Rectangle(double h,double l) {
        this.setHeight(h);
        this.setLenth(l);
    }
    private void setLenth( double h) {
        // TODO 自动生成的方法存根
        this.h=h;
    }
    public double getHeight() {
        return h;
    }
    private void setHeight(double l) {
        // TODO 自动生成的方法存根
        this.l=l;
    }
    public double getLenth() {
        return l;
    }
    public double getArea() {
        return h*l;
    }
}

class Circle extends Shape{
    double r;
    public Circle(double r) {
        this.setRadius(r);
    }

    public  void setRadius(double r) {
        this.r=r;
    }
    public double getRadius() {
        return r;
    }
    public double getArea() {
        return Math.PI*Math.pow(r,2);
    }
}

public class text{
     public static void main(String[] args) {
            Shape rec = new Rectangle(15,15);
            Shape tri = new Triangle(5,5,5);
            Shape cir = new Circle(7);
            System.out.println("矩形面积:"+rec.getArea());
            System.out.println("三角形面积:"+tri.getArea());
            System.out.println("圆面积:"+cir.getArea());
        }
}

2.运行结果

(二)使用接口技术

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

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

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

1.代码

package test;

import java.util.Scanner;

interface Shape1{
    public void size();
    public abstract double getArea();
    public abstract double getPerimeter();
}

 class Line implements Shape1{
    private double Lenth;
    public Line(double Lenth) {
        this.setLenth(Lenth);
    }
    public double getLenth() {
        return Lenth;
    }
    public void setLenth(double Lenth) {
        this.Lenth = Lenth;
    }

    public double getPerimeter() {
        return this.Lenth;
    }
    
    public double getArea() {
        return 0;
    }
    @Override
    public void size() {
        // TODO 自动生成的方法存根
        System.out.println("直线的周长:" + getPerimeter());
        System.out.println("直线的面积:" + getArea());
    }
}
 
 class Circle1 implements Shape1{
     private double r;
     public Circle1(double r) {
         this.setR(r);
     }
    public double getR() {
        return r;
    }
    public void setR(double r) {
        this.r = r;
    }
    public double getArea() {
        return Math.PI*Math.pow(r, 2);
    }
    public double getPerimeter() {
        return Math.PI*r*2;
    }
    public void size() {
        System.out.println("圆的周长:" + getPerimeter());
        System.out.println("圆的面积:" + getArea());
    }
 }
 
 public class test1{
     public static void main(String[] args) {
            Shape1 sha1=new Circle1(6);
            Shape1 sha2=new Line(7);
            sha1.size();
            sha2.size();
        }
 }

2.运行结果

总结:
抽象方法必须用abstract关键字进行修饰。如果一个类含有抽象方法,则称这个类为抽象类,抽象类必须在类前用abstract关键字修饰。
接口是公开的,里面不能有私有的方法或变量,是用于让别人使用的,而抽象类是可以有私有方法或私有变量的,另外,实现接口的一
定要实现接口里定义的所有方法,而实现抽象类可以有选择地重写需要用到的方法,一般的应用里,最顶级的是接口,然后是抽象类实
现接口,最后才到具体类实现。

 posted on 2019-10-12 19:35  有点无奈  阅读(148)  评论(0编辑  收藏  举报