zoukankan      html  css  js  c++  java
  • 第八周课程总结及实验报告

    实验报告

    一、编写一个类,在其main()方法中创建一个一维数组,在try字句中访问数组元素,使其产生ArrayIndexOutOfBoundsException异常。在catch子句里捕获此异常对象,并且打印“数组越界”信息,加一个finally子句,打印一条信息以证明这里确实得到了执行。

    1)实验代码

    package text7;
    
    public class Text7 {
    
    	public static void main(String[] args) {
    		
    		
    		try {
    			int s[]= {1,2,3};
    			System.out.println(s[3]);
    		}catch(ArrayIndexOutOfBoundsException a) {
    			System.out.println("数组越界"+a);
    		}finally {
    			System.out.println("不管是否出现异常,都执行此行代码");
    		}
    		
    		System.out.println("计算结束");
    
    	}
    
    }
    

    2)运行截图

    二、车站检查危险品的设备,如果发现危险品会发出警告。编程模拟设备发现危险品。

    技术方案:

    编写一个Exgeption的子类DangerException,该子类可以创建异常对象,该异常对象调用toShow()方法输出“危险物品”。编写一个Machine类,该类的方法checkBag(Goods goods)当发现参数goods是危险品时(goods的isDanger属性是true)将抛出DangerException异常。
    程序在主类的main()方法中的try-catch语句的try部分让Machine类的实例调用checkBag(Goods goods)的方法,如果发现危险品就在try-catch语句的catch部分处理危险品。

    1)实验代码

    package text71;
    
    public class DangerException extends Exception {
    	private static final long serialVersionUID = 1L;
    	
    	private String message;
    	
    	public DangerException(String message) {
    		super();
    		this.message = message;
    	}
    
    	public String getMessage() {
    		return message;
    	}
    
    	public void setMessage(String message) {
    		this.message = message;
    	}
    
    	public void toShow() {
    		System.out.println("危险物品:"+message);
    	}
    	
    }
    
    
    package text71;
    
    public class Goods {
    	
    	private boolean isDanger;
    	private String name;
    	
    	public boolean isDanger() {
    		return isDanger;
    	}
    	
    	public void setDanger(boolean isDanger) {
    		this.isDanger = isDanger;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	
    	public void setName(String name) {
    		this.name = name;
    	}
    	
    	public Goods(String name) {
    		this.name = name;
    	}
    	
    
    }
    
    package text71;
    
    public class Machine {
    
    	
    	public void checkBag(Goods goods) throws DangerException{
    		if(goods.isDanger()==true) {
    			throw new DangerException(goods.getName());
    		}
    		else {
    			System.out.println(goods.getName()+":不是危险品");
    		}
    	}
    }
    
    package text71;
    
    import java.util.ArrayList;
    
    public class Text7 {
    
    	public static void main(String[] args) {
    		ArrayList<String> list=new ArrayList<String>();
    		
    		list.add("汽油");
    		list.add("香烟");  
    		list.add("枪");
    		list.add("面包");      
    		list.add("酒精");
    		list.add("刀具");
    		
    		String name=list.get(5);
    		Goods good = new Goods(name);
    		good.setDanger(true);
    		
    		
    		Machine mc=new Machine();
    		
    		try {
    			mc.checkBag(good);
    			System.out.println(good.getName() + ":检查通过");
    		}catch(DangerException e) {
    			e.toShow();
    			System.out.println(good.getName() + ":未通过");
    		}
    		
    	}
    
    }
    

    2)运行截图

    学习总结

    异常类

    throwsthrow

    throws

    使用throws声明的方法表示此方法不处理异常,而交给方法的调用处进行处理,格式如下:

    public 返回值类型 方法名称(参数列表...)throws 异常类{}
    

    throw

    与throws关键字不同的是,可以直接使用throw关键字抛出一个异常,抛出时直接抛出异常类的实例化对象即可。

    try{
          throw new Exception("自己抛出的异常!");
    }catch(Exception e){
          System.out.println(e);
    }
    

    多线程

    多线程的实现有两种方法继承Thread类使用Runnable接口
    Thread类不共享,Runnable接口共享。

    Thread类
    格式:

    class 类名称 extends Thread{
          属性...;
          方法...;
          public void run(){
                 线程主体;
        }
    }
    

    想要实现多线程不是调用run()方法,而是start()方法。

  • 相关阅读:
    poj 3261 Milk Patterns
    poj 3292 Semi-prime H-numbers
    bzoj千题计划256:bzoj2194: 快速傅立叶之二
    bzoj千题计划255:bzoj3572: [Hnoi2014]世界树
    bzoj千题计划254:bzoj2286: [Sdoi2011]消耗战
    bzoj千题计划253:bzoj2154: Crash的数字表格
    扩展BSGS算法
    bzoj千题计划252:bzoj1095: [ZJOI2007]Hide 捉迷藏
    bzoj千题计划251:bzoj3672: [Noi2014]购票
    bzoj千题计划250:bzoj3670: [Noi2014]动物园
  • 原文地址:https://www.cnblogs.com/H-Alice/p/11692954.html
Copyright © 2011-2022 走看看