第八周课程总结 & 实验报告(六)

第八周课程总结

一、包装类
      介绍
       装箱与拆箱
       应用
二、异常
      基本概念
       基本格式
       异常类的继承结构
       throws关键字
       throw关键字
       Exception类和RuntimeException类
       自定义异常类
       
三、多线程
基本概念
进程
多线程
Java中线程的实现
继承Thread类
实现Runnable接口
Thread和Runnable的区别

一、包装类

       1.介绍

                               8种基本类型变包装类

(1)除了Character 和 Boolean,其它六种都属于Number类的子类。

(2)Character 和 Boolean属于Object类的直接子类

2.装箱与拆箱

      装箱:将一个基本数据类型变为包装类

      拆箱:将一个包装类变为基本数据类型

3.应用

   (1)Integer类(字符串转int型)

public static int pareInt(String s) throws NumberFormatException

         (2)Float类(字符串转float型)

public static float pareFloat (String s) throws NumberFormatException

二、异常
                 1.基本概念

         异常是导致程序中断的一种指令流,如果不对异常进行正确的处理,则可能导致程序中断执行。

                     2.基本格式

            

try{
     //可能出现异常的语句
}[catch{
          //编写异常的处理语句
}catch{
          //编写异常的处理语句
}.......]
finally{
          //一定会运行到的程序代码;
}

异常处理流程

         3.异常类的继承结构

            在整个Java的异常结构中,实际有两个最常用的类,分别为Exception和Error,

            这两个全是Throwable的子类

             Exception:一般表示程序中出现的问题,可以直接使用try....catch处理。

             Error:一般指的是JVM错误,程序中无法处理。

        

         4.throws关键字

           在定义一个方法时可以使用throws关键字声明,使用throws声明的方法表示此方法

           不处理异常,而交给方法的调用者进行处理。

【格式】

class Math{
           public int div(int i,int j) throws Exception{    //方法可以不处理异常
                     int temp = i/j;
                     return temp;
           }

}

         5.throw关键字

                        使用throw抛出一个异常,抛出时直接抛出异常类的实例化对象即可

【格式】

public class ThrowDemo{
          public static void main(String args[]){
                  try{
                       throw new Exception("自己抛出异常");    //抛出异常的实例化对象
                  } catch(Exception e){
                           System.out.println(e);
                   }
           }
}

         6.Exception类和RuntimeException类

Exception: 在程序中必须使用try....catch进行处理

RuntimeException:可以不使用,但如果有异常产生,则异常将由JVM进行处理

         7.自定义异常类

           根据自己的需求定义自己的异常类


class
MyException extends Exception{ public MyException(String msg){ super(msg) } } public class ThrowDemo{ public static void main(String args[]){ try{ throw new Exception("自定义异常"); //抛出异常的实例化对象 } catch(Exception e){ System.out.println(e); } } }

 三、多线程
         1.基本概念
                   (1)进程:进程是程序的一次动态执行过程
                   (2)多线程:多线程是实现并发机制的一种有效手段


         2.Java中线程的实现
                     (1)继承Thread类

【语法】

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

 

                     (2)实现Runnable接口

【语法】

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

 

                     (3)Thread和Runnable的区别

                        1.如果通过继承Thread类实现多线程,需要覆写run()方法

                         2.如果继承Thread类则不适合用于多个线程共享资源,

                           而实现Runnable接口,可以方便实现

实验报告(六)

一、异常

(一)实验源码

package yichang;

import java.util.*;
public class Yichang {

    public static void main(String[] args) {
        int score[]=new int [6];
        System.out.println("程序开始");
        System.out.println("请输入下标");
        Scanner out = new Scanner(System.in);
        int n = out.nextInt();
        try{
            for(int i=0;i<6;i++){
                score[i]=i*i;
                
            }
            System.out.println("score["+n+"]="+score[n]+" ");
        }catch(ArrayIndexOutOfBoundsException a){
            System.out.println("数组越界:"+a);
        }finally{
            System.out.println("程序结束");
        }
    }

}

(二)实验结果

二、危险品检查

(一)实验源码

异常类
package
danger; public class DangerException extends Exception{ String imformation; DangerException(String imformation){ this.imformation=imformation; } void toShow(){ System.out.println(imformation); } }
检查类
package
danger; public class Machine{ String name; Goods g; public boolean isDanger(String name) { String score[] = {"炸弹","毒药","刀具","枪支"}; boolean flag =false; for(int i=0;i<score.length;i++) { if(name.equals(score[i])) { flag = true; break; } } return flag; } void checkBag(Goods g){ this.g=g; name=g.getName(); try{ if(isDanger(name)){ System.out.print(name); throw new DangerException("是危险品!!!"+" "); } else{ System.out.print(name); throw new DangerException("不是危险品!"+" "); } }catch(DangerException e){ e.toShow(); } } }
Goods类
package
danger; public class Goods{ String name; public void setName(String name){ this.name=name; } public String getName(){ return name; } }

测试类
package
danger; import java.util.Scanner; public class Test { public static void main(String[] args) { while(true) { Scanner sc=new Scanner(System.in); System.out.println("请输入物品:"); String input=sc.nextLine(); Goods g=new Goods(); g.setName(input); Machine m=new Machine(); m.checkBag(g); } } }

(二)实验结果

        

原文地址:https://www.cnblogs.com/xu23/p/11693036.html