第七周作业

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

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

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

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

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

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

package Fuck1;

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 Fuck2{
     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 Fuck1;

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 Fuck2{
    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());
    }
}

学习总结:
父类和子类,抽象类不能被实例化,额还有一个子类只能存在一个父类;

posted @ 2019-10-12 17:42  dxl1314520  阅读(165)  评论(0编辑  收藏  举报