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


    实验四 类的继承

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

     

    • 实验内容

    (一)抽象类的使用

    1. 设计一个类层次,定义一个抽象类--形状,其中包括有求形状的面积的抽象方法。 继承该抽象类定义三角型、矩形、圆。 分别创建一个三角形、矩形、圆存对象,将各类图形的面积输出。
      注:三角形面积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接口。分别创建一个“直线”、“圆”对象,将各类图形的大小输出。

    1. 编程技巧

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

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

    (一)源代码

    abstract class shape {
         public abstract double print();
    }
    
    class Triangle extends shape{
        private double a;
        private double b;
        private double c;
        public Triangle(double a,double b,double c) {
            this.a=a;
            this.b=b;
            this.c=c;
    }
        public double print() {
            double p=(a+b+c)/2;
            return Math.sqrt(p*(p-a)*(p-b)*(p-c));
        }
    }
    
    class Rectangle extends shape{
        private double width;
        private double height;
        public Rectangle(double width,double height) {
            this.height=height;
            this.width=width;
        }
        
        public double print() {
            return width*height;
        }
    }
    
    class Circle extends shape{
        double radious;
        public Circle(double radious) {
            this.radious=radious;
        }
        public double print() {
            return Math.PI*radious*radious;
        }
    }
    
    public class Jack{
        public static void main(String[] args) {
            shape arae1=new Triangle(6,8,10);
            shape area2=new Rectangle(6,10);
            shape area3=new Circle(3);
            System.out.println("三角形的面积:"+area1.print());
            System.out.println("矩形的面积:"+area2.print());
            System.out.println("圆的面积:"+area3.print());
        }
    }

    运行结果

    (二)源代码

    interface Shape{
       public abstract void size();
    }
    
    class Straight implements Shape{
        private double figure;
        public Straight(double figure) {
            this.figure=figure;
        }
        public void size() {
            System.out.println("直线的大小:"+figure);
        }
    }
    
    class Circle implements Shape{
        private double radius;
        public Circle(double radius) {
            this.radius=radius;
        }
        public void size() {
            System.out.println("圆的面积:"+Math.PI*radius*radius);
        }
    }
    
    public class Jack{
        public static void main(String[] args) {
            Shape size1=new Straight(9);
            Shape size2=new Circle(5);
            size1.size();
            size2.size();
        }
    }

    运行结果

     总结:第一题的步骤就是  定义抽象方法——>子类继承抽象方法——>子类调用抽象方法——>通过子类实例化抽象方法。

    第二题的方法其实也是一样只是子类是通过接口实现继承的。

    学习总结:

    1、抽象类

     2、接口定义:

    实现接口:

  • 相关阅读:
    hdoj 2544 最短路径
    树状数组 hdoj1166
    并查集学习
    1402大数相乘 FFT算法学习!
    hdu1014
    动态规划 简单题dp
    迷宫路径
    简单的动态规划dp
    poj 3282 (bFS)
    背包问题,看不懂,啊!!!!!!!!dp
  • 原文地址:https://www.cnblogs.com/JokerXue/p/11664050.html
Copyright © 2020-2023  润新知