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) 通过父类引用子类对象,通过该引用访问对象方法时实际用的是子类的方法。可将所有对象存入到父类定义的数组中。

    (二)使用接口技术

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

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

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

    抽象类实验代码

    package Work2;
    
    public abstract class Abstract {
    	 public void area() {
    	        System.out.println(this.getArea());
    	    }
    
    	    public abstract double getArea();
    }
    
    public class triangle extends Abstract {
    	private double a;
        private double b;
        private double c;
        private double p=(a+b+c)/2;
        private double s=Math.sqrt(p*(p-a)*(p-b)*(p-c));
        public triangle(double a,double b,double c) {
            this.a=a;
            this.b=b;
            this.c=c;
            this.p=(a+b+c)/2;
            this.s=Math.sqrt(p*(p-a)*(p-b)*(p-c));
        }           
        public double getArea() {
            return s;
        }
    }
    
    public class rectangle extends Abstract {
    	private double width;
        private double height;
        public rectangle(double width,double height) {
            this.width=width;
            this.height=height;
        }
        public double getArea() {
            return width*height;
        }
    }
    
    public class circle extends Abstract {
    	private double radius;
        public circle(double r) {
            this.radius=r;
        }
        public double getArea() {
            return Math.PI*radius*radius;
        }
    }
    
    public class test {
    	 public static void main(String[] args) {
    		 triangle alita1=new triangle(3,4,5);
    	        alita1.area();
    	     rectangle alita2=new rectangle(4,5);
    	        alita2.area();
    	     circle  alita3=new circle(10);
    	        alita3.area();
    	    }
    }
    

    实验结果

    接口类实验代码

    package Work3;
    
    public interface shape {
    	 public void size();
    
    }
    
    class straightline implements shape{
        private double length;
        public straightline(double length) {
            this.length=length;
        }
        public void size() {
            System.out.println("直线的长度:"+length);
        }
    
    }
    
    public 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 test {
    	public static void main(String[] args) {
            shape a=new straightline(100);
            shape b=new circle(10);
            a.size();
            b.size();
    	}
    }
    

    实验结果

    实验总结

    本次实验的内容为抽象类和接口类的实际运用,老师上课和我们讲解的很清楚,所以运用的还算流畅

    课程总结

    本周我们学习了抽象类和接口类

    接口类和抽行类的运用


    Object类

    Obiect类是所有类的父类。



    还可以运用tostring方法取得对象

  • 相关阅读:
    ASP.NET动态生成控件
    WGCLOUD如何禁用指令下发功能
    C#基于NAudio工具对Wav音频文件剪切(限PCM格式)
    C#多线程与多任务
    我的JQuery插件 Confirmer
    我的JQuery插件 submenu
    关于jQuery在asp.net中使用ajax的探讨
    发布jQuery表单验证插件 JQuery.validator.js
    TreeView递归绑定地区列表
    Uploadify(JQuery上传插件)在asp.net中使用例子
  • 原文地址:https://www.cnblogs.com/xzy999123/p/11663599.html
Copyright © 2011-2022 走看看