实验六内部类和异常类1.实验目的1、掌握内部类的常用方法2、掌握匿名类的常用方法3、掌握接异常类的常用方法2.实验内容1、根据附录里的源代码,按照注释要求,完成代码填空,使程序能够运行得出结果。
1)实验1 红牛农场package four;public class RedCowForm {String formName;RedCow cow;//【代码1】//用内部类RedCow声明对象cowRedCowForm(){}RedCowForm(String s){cow=new RedCow(150,112,5000);//【代码2】//创建cowformName=s;}public void showCowMess(){cow.speak();}class RedCow{String cowName="红牛";int height,weight,price;RedCow(int h,int w,int p){height=h;weight=w;price=p;}void speak(){System.out.println(cowName+",身高:"+height+"cm,体重:"+weight+"kg");System.out.println("生活在"+formName);}}}package four;public class MainClass {public static void main(String[] args) {RedCowForm form=new RedCowForm("红牛农场");form.showCowMess();}}2)实验2 检查危险品package four;public class Goods {boolean isDanger;String name;public boolean isDanger() {return isDanger;}public void setDanger(boolean boo) {isDanger = boo;}public String getName() {return name;}public void setName(String s) {name = s;}}package four;public class DangerException extends Exception{ String message;public DangerException(){message="危险品!";}public void toShow(){System.out.print(message+"");}}package four;public class Machine {Goods[] goods;public void checkBag(Goods goods) throws DangerException{ if(goods.isDanger){throw new DangerException();//【代码1】//用throw抛出一个DangerException的匿名类}}}package four;public class Check {public static void main(String[] args) {Machine machine=new Machine();String name[]={"苹果","炸药","西服","硫酸","手表","硫磺"};Goods [] goods= new Goods[name.length];//检查6件物品for(int i=0;i<name.length;i++){goods[i]=new Goods();if(i%2==0){goods[i].setDanger(false);goods[i].setName(name[i]);}else{goods[i].setDanger(true);goods[i].setName(name[i]);}}for(int i=0;i<goods.length;i++){try{machine.checkBag(goods[i]);System.out.println(goods[i].getName()+"检查通过");}catch(DangerException e){e.toShow();//【代码2】//e调用toShow()方法System.out.println(goods[i].getName()+"被禁止!");}}}}3)实验3 内部类的使用方法package four;public class Outer{private static int size;/** 内部类Inner的声明 */public class Inner{private int size;/** 方法doStuff() */public void doStuff(int size){size=size+3;//【代码1】 //存取局部变量size加3this.size=size+2;//【代码2】 //存取其内部类的成员变量size加2Outer.this.size=size+1;//【代码3】存取其外部类的成员变量size加1System.out.println(size+" "+this.size+" "+Outer.this.size);}}//内部类Inner结束/** 类Outer中定义的实例方法testInner()方法 */public void testInner(){Inner i=new Inner();i.doStuff(size);//调用Inner类中的doStuff方法}/** main()方法 */public static void main(String args[]){Outer o=new Outer();o.testInner();//调用Outer类中的testInner方法}}//类Outer结束4)实验4 匿名类的使用方法package four;interface Square{ double getSquare(int n);}interface Sqrt{ public double getSqrt(int x);}class A{ void p(Square square){System.out.println(square);//输出getSquare方法计算后的结果}void k(Sqrt sqrt){System.out.println(sqrt);//输出getSqrt方法计算后的结果}}public class Anonymity{ public static void main(String args[]){A a=new A();a.p(null);//实现Square接口,重写getSquare方法,返回n的平方Sqrt sqrt=new Sqrt(){public double getSqrt(int x) {return Math.sqrt(x);}};//重写getSqrt方法,返回x的开平方根,用静态Math.sqrt方法来运算 System.out.println("计算开平方:"+sqrt.getSqrt(5));}}5)实验5 异常类使用方法package four;class MyException extends Exception{String message;MyException(String str){message=str;//把参数的引用传给message成员变量}public String getMessage(){return message;//返回message成员变量}}abstract class C{abstract int f(int x,int y) throws MyException;}class B extends C{int f(int x,int y) throws MyException{if(x<100&&y<100)//判断x、y都不大于100{throw new MyException("乘数超过100");//抛出一个新建的MyException对象,MyException对象中构造方法传入的参数内容为:乘数超过100}return x*y;}}public class eClass{public static void main (String args[]){C a;a = new B();//设计a对象的B类上转型对象try{System.out.println(a.f(12,10));a.f(150,250);//使用f()方法时传入一个大于100的乘数}catch(MyException e){System.out.println("x、y都不大于100");//输出错误信息 }}}2、设计编写程序完成以下任务。
1)假如要从命令行获得两个整数,自定义两个异常类来描述可能发生的异常:ParameterNumberException(参数个数异常),ParameterFormateException (参数格式异常),设计一个类,在check(String args[])方法中通告这两个异常,在main方法中判断相应的情况下触发异常,然后捕获异常,对它们进行处理。
(知识点:异常处理机制)判断长度和类型不准,相应条件分支抛出对应的自定义异常即可2)编写一个异常类MyException,在编写一个类Student,该类有一个产生异常的方法public void speak(int m)throws MyException,要求参数m的值大于1000时,方法抛出一个MyException对象。
最后编写一个主类,在主类的main方法中用Student创建一个对象,让该对象调用speak方法。
3.实验步骤略4.评分标准1.A——内容功能完善,编程风格好,人机接口界面好;2.B——内容功能完善,编程风格良好,人机接口界面良好;3.C——完成必做内容;4.D——能完成必做内容;5.E——未按时完成必做内容,或者抄袭(雷同者全部为E).参照书上实验按模版要求,将【代码】替换为Java程序代码,编写好完整的程序文档,最后运行得到的相关文件,把实验所得文件一起打包上交。