zoukankan      html  css  js  c++  java
  • 第七周课程总结&实验报告(五)


    实验四 类的继承

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

    (一)抽象类的使用

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

    2.编程技巧

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

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

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

    实验代码:
    import java.util.Scanner;
    
    abstract class Shapes
    {
      public abstract void area();}
    
    class Triangle extends Shapes{
                 private double a,b,c;
                 private double p;
         
          Triangle(double a, double b, double c, double p){
                 this.a = a;
                 this.b = b;
                 this.c = c;
                 this.p = (a+b+c)/2;}
        public void area() 
            {
                System.out.println(Math.sqrt(p*(p-a)*(p-b)*(p-c)));
            }
      }
    class Rectangle extends Shapes{
                 private double h,g;
                 Rectangle(double h, double g){
                   this.h = h;
                   this.g = g;
               }
            public void area() {
                 System.out.println(h*g);   
            }
        }
    class Cricle extends Shapes{
                 private double r;
                 Cricle(double r){
                    this.r=r;
             }
      public void area()
             {
                 System.out.println(Math.pow(r,2)*Math.PI);
            }
        }
           public class shape{
    public static void main(String[] args) 
    {
                Scanner boniu=new Scanner(System.in);
                System.out.println("triangle length a:" );
                double a=boniu.nextDouble();
                System.out.println("triangle length b:" );
                double b=boniu.nextDouble();
                System.out.println("triangle length c:" );
                double c=boniu.nextDouble();
                Triangle triangle=new Triangle(a,b,c,(a+b+c)/2);
                System.out.println("area of triangle:");
                triangle.area();
    
                System.out.println("length of rectangle h:");
                double h=boniu.nextDouble();
                System.out.println("width of rectangle g:");
                double g=boniu.nextDouble();
                Rectangle rectangle=new Rectangle(h,g);
                System.out.println("area of rectangle:");
                rectangle.area();
                
                System.out.println("radius of circle:");
                double r=boniu.nextDouble();
                Cricle cricle=new Cricle(r);
                System.out.println("area of circle:");
                cricle.area();
    
            }
        }

    运行结果:

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

    1. 编程技巧

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

    实验代码:

    import java.util.Scanner;

    interface Shape

    {
          void size();}

    class line implements Shape

    {
          private double length;
         
               line(double length) {
               this.length=length;
                 }
              
          public void size(){
               System.out.println(length);
              
        }
    }

    class cirlce implements Shape{
       
                           private double radius;
                           cirlce(double radius){
                               this.radius=radius;
        }
          public void size(){
               System.out.println(Math.PI*2*radius);
        }
    }

          public class a{
             
                    public static void main(String[] args)
                      {
                            System.out.println("line length");
                            Scanner dd=new Scanner(System.in);
                            double length=dd.nextDouble();
                            line line=new line(length);
                            System.out.println("line size:");
                                     line.size();
                                    

               System.out.println("radius of circle");
               double radius=dd.nextDouble();
               cirlce cirlce=new cirlce(radius);
               System.out.println("ciecumference:");
                                     cirlce.size();
           
        }
    }

     运行结果:

    实验总结:

     1、在接口中定义的方法在实现接口的具体类中需要重写实现

     2、利用接口类型的变量可引用实现该接口的类创建的对象
     
     
     
  • 相关阅读:
    性能测试-地铁模型分析
    如何测试网页登录页面
    软件测试修炼之道(转载)
    每当在测试之路迷茫的时候来看看这篇文章
    软件测试博客较好的网址(供参考)
    Spring扩展:替换IOC容器中的Bean组件 -- @Replace注解
    拯救老旧工程,记桥接SpringMVC与Stripes框架
    Netty实现WebSocket,URI参数问题
    高性能内存队列Disruptor--原理分析
    Idea中Smart Tomcat插件启动报NullPointerException问题
  • 原文地址:https://www.cnblogs.com/wangzihaojun/p/11657139.html
Copyright © 2011-2022 走看看