第七周

实验四 类的继承

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

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

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

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

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

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

package hello;

import java.util.Scanner;

abstract class Shape {
    public abstract double  Area();
}
class Triangle extends Shape{
    private int a,b,c;
    public Triangle() {}
    public Triangle(int a,int b,int c){
        this.a = a;
        this.b = b;
        this.c = c;
    }
    public int getA() {
        return a;
    }
    public void setA(int a) {
        this.a = a;
    }
    public int getB() {
        return b;
    }
    public void setB(int b) {
        this.b = b;
    }
    public int getC() {
        return c;
    }
    public void setC(int c) {
        this.c = c;
    }
    public double Area() {
        double p = (a+b+c)/2;
        return  Math.sqrt(p*(p-getA())*(p-getB())*(p-getC()));
        
    }
}


class Rectangle extends Shape{
    private double height,width;
    
    public Rectangle() {}
    public Rectangle(double height,double width){
        this.height = height;
        this.width = width;
    }
    
    public double getHeight() {
        return height;
    }
    public void setHeight(double height) {
        this.height = height;
    }
    public double getWidth() {
        return width;
    }
    public void setWidth(double width) {
        this.width = width;
    }
    public double Area() {
        return getHeight()*getWidth();
    }
}


class Circle extends Shape{
    private double radius;
    
    public Circle(){}
    public Circle(double radius) {
        this.radius = radius;
    }
    public double getRadius() {
        return radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    public double Area() {
        return Math.PI*Math.pow(getRadius(), 2);
    }
}

public class Demo2{
     public static void main(String[] args) {
         System.out.println("请输入三角形的边长:");
         Scanner s1 = new Scanner(System.in);
         int a = s1.nextInt();
         int b = s1.nextInt();
         int c = s1.nextInt();
         Triangle triangle = new Triangle(a,b,c);
         if(a+b>c && a+c>b && b+c>a) {
         System.out.println("三角形的面积为:"+triangle.Area());
         }
         else {
             System.out.println("这不是一个三角形,无法输出面积。");
         }
         
         Scanner s2 = new Scanner(System.in);
         System.out.println("请输入矩形的长和宽:");
         double height = s2.nextDouble();
         double width = s2.nextDouble();
         Rectangle rectangle = new Rectangle(height,width);
         System.out.println("矩形的面积为:"+rectangle.Area());
         
         
         Scanner s3 = new Scanner(System.in);
         System.out.println("请输入圆的半径:");
         double radius = s3.nextDouble();
         Circle circle = new Circle(radius);
         System.out.println("圆的面积为:"+circle.Area());
         
     }
}

截图:

(二)使用接口技术

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

2、编程技巧

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

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

package hello;

import java.util.Scanner;

interface Shape{
    public void Size();
}
class Line implements Shape{
    private double length;
    public Line() {}
    public Line(double length) {
        this.setLength(length);
    }
    
    @Override
    public void Size() {
        System.out.println("直线的长度:"+getLength());
    }
    public double getLength() {
        return length;
    }
    public void setLength(double length) {
        this.length = length;
    }
    
}
class Circle implements Shape{
    private double radius;
    public Circle(double radius) {
        this.radius = radius;
    }
    
    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }
    public double getArea() {
        return Math.PI*Math.pow(radius, 2);
    }
    @Override
    public void Size() {
        System.out.println("圆的面积:"+getArea());
    }
}
public class Demo1{
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("请输入直线的长度:");
        int length = s.nextInt();
        Line line = new Line(length);
        System.out.println("直线长度为:"+line.getLength());
        
        Scanner p = new Scanner(System.in);
        System.out.println("请输入圆的半径:");
        double radius = p.nextDouble();
        Circle circle = new Circle(radius);
        System.out.println("圆的面积为:"+circle.getArea());
    }
}

截图:

学习总结:

1、抽象类与接口

(1)抽象类:抽象类必须用 abstract 修饰,子类必须实现抽象类中的抽象方法,如果有未实现的,那么子类也必须用 abstract 修饰。抽象类默认的权限修饰符为 public,可以定义为 public 或 procted,如果定义为 private,那么子类则无法继承。抽象类不能创建对象

(2)接口:接口中的变量隐式的使用 public static final 修饰,并且需要给出初始值。方法隐式的使用 public abstract 修饰(并且只能是 public ,如果是 private,procted,那么就编译报错)。

(3)接口和抽象类的区别:

a.抽象类只能继承一次,但是可以实现多个接口
b.接口和抽象类必须实现其中所有的方法,抽象类中如果有未实现的抽象方法,那么子类也需要定义为抽象类。抽象类中可以有非抽象的方法
c.接口中的变量必须用 public static final 修饰,并且需要给出初始值。所以实现类不能重新定义,也不能改变其值。
d.接口中的方法默认是 public abstract,也只能是这个类型。不能是 static,接口中的方法也不允许子类覆写,抽象类中允许有static 的方法

2、Object类
(1)toString():将一个对象转换成字符串形式体现出来,也是最常用的一个方法
(2)equals():制定一个类型的比较规则
(3)hashCode():生成一个对象的散列码

原文地址:https://www.cnblogs.com/zhouhuahua/p/11663457.html