实验03 常用类及异常类 2

上传人:第*** 文档编号:31135933 上传时间:2018-02-05 格式:DOC 页数:12 大小:243.50KB
返回 下载 相关 举报
实验03 常用类及异常类 2_第1页
第1页 / 共12页
实验03 常用类及异常类 2_第2页
第2页 / 共12页
实验03 常用类及异常类 2_第3页
第3页 / 共12页
实验03 常用类及异常类 2_第4页
第4页 / 共12页
实验03 常用类及异常类 2_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《实验03 常用类及异常类 2》由会员分享,可在线阅读,更多相关《实验03 常用类及异常类 2(12页珍藏版)》请在金锄头文库上搜索。

1、实验三 内部类、常用类及异常类的用法1实验目的(1)掌握 Java 中内部类的用法、异常类的用法(2)掌握 Java 中 String 类常用方法、StringBuilder 类的用法;(3)掌握 System 类、Date 类、Calender 类、DateFormat 类、NumberFormat 类、Random 类与 BigInteger 及 BigDecimal 类的用法;(4)掌握 Java 中正则表达式的基本用法;2实验内容实验题 1 修改实验二实验题 5,声明一个局部变量 String text = ;然后通过循环把数组中的成员(有序)添加到 text 中,修改方法 JOpti

2、onPane.showMessageDialog();参数以显示text。实验题 2 用 StringBuiler text = ;替换 String text = ;然后通过循环使用 StringBuiler类的 append 方法向 text 中添加把数组中的成员(有序)添加到 text 中,修改方法JOptionPane.showMessageDialog();参数以显示 text。思考问题:对比分析 StringBuiler 与 String 的区别。(1)不可改变对象 在.NET 中 String 是不可改变对象,一旦创建了一个 String 对象并为它赋值,它就不可能再改变。(2)

3、引用类型 前面说过 String 是引用类型,这就是如果我们创建很多个相同值的字符串对象,它在内存中的指向地址应该是一样的。也就是说,当我们创建了字符串对象 a,它的值是“1234”,当我们再创建一个值为“1234”的字符串对象 b 时它不会再去分配一块内存空间,而是直接指向了 a 在内存中的地址。(3)StringBuilder 对象 通过上面的分析可以看出,String 类型在做字符串的连接操作时,效率是相当低的,并且由于每做一个连接操作,都会在内存中创建一个新的对象,占用了大量的内存空间。这样就引出 StringBuilder 对象,StringBuilder 对象在做字符串连接操作时是

4、在原来的字符串上进行修改,改善了性能。实验结果:Main 函数:package cn.edu.nwsuaf.jp.p4;import java.util.Arrays;import javax.swing.JOptionPane;import cn.edu.nwsuaf.jp.p4.data.Mobile;import cn.edu.nwsuaf.jp.p4.data.Mp3Player;import cn.edu.nwsuaf.jp.p4.data.Product;public class Store public static int count = 0;public static voi

5、d main(String args) Mp3Player p1 = new Mp3Player(Meizo X3 (256MB),399.0f);Mp3Player p2 = new Mp3Player(Meizo E5 (512MB),580.0f);Mp3Player p3 = new Mp3Player(Xlive XM Mp3Play(256MB),930.0f);Mobile m1 = new Mobile(E365 on China Mobile,1780.0f);Mobile m2 = new Mobile(E3330 on China Mobile,1450.0f);Prod

6、uct products = p1,p2,p3,m1,m2;Arrays.sort(products);StringBuilder text = new StringBuilder() ; for(int index = 0; index 100)throw new OverException(成绩大于 100 了!);public static void main(String args) int a = 1;int Pass = 0;int notPass = 0;System.out.println(请输入学生成绩,最后以-1结束);System.out.print(第 + a + 位学

7、生的成绩:);Scanner reader = new Scanner(System.in);double sum = 0;double x = reader.nextDouble();while (x != -1) try score(x);if (x = 60) Pass += 1;sum += x; catch (MinusException e1) System.out.println(e1);a-=1; catch (OverException e2) System.out.println(e2);a-=1;a = a + 1;System.out.print(第 + a + 位学生

8、的成绩:);x = reader.nextDouble();System.out.println(平均分为: + sum / (notPass + Pass);System.out.println(及格人数为: + Pass);System.out.println(不及格人数为: + notPass);实验题 6 设计类 ReflectTester,该类中有一方法 copy(Object obj) ,该方法能够创建一个和参数 obj 同样类型的对象,然后把 obj 对象中所有属性复制到新建对象中,并将它返回。设计思路:首先定义一个类 Product,有属性 name 和 price,以及构造函

9、数和getName()和 getPrice()函数。再定义一个 reflectTester 类,实现 copy 方法,在主函数中定义一个 Product 实例,并调用 copy 方法。实验结果:代码如下:package cn.edu.reflect.objectcopy;public class Product private String name;private float price;public String getName() return name;public void setName(String name) this.name = name;public float getP

10、rice() return price;public void setPrice(float price) this.price = price;public Product(String name, float price) super();this.name = name;this.price = price;public Product() super();package cn.edu.reflectTester;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import

11、 java.lang.reflect.Method;import cn.edu.reflect.objectcopy.Product;import sun.reflect.generics.tree.ClassTypeSignature;SuppressWarnings(unused)public class reflectTester SuppressWarnings( rawtypes, unchecked )public Object copy(Object object) throws InstantiationException, IllegalAccessException, Il

12、legalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityExceptionClass classType = object.getClass();System.out.println(Class + classType.getName();Object objectOfCopy = classType.getConstructor(new Class).newInstance(new Object);Field fields = classType.getDeclaredFields()

13、;for(Field field:fields)String fieldName = field.getName();String firstLetter = fieldName.substring(0,1).toUpperCase();String getMethodName = get + firstLetter + fieldName.substring(1);String setMethodName = set + firstLetter + fieldName.substring(1);Method getMethod = classType.getMethod(getMethodN

14、ame, new Class);/Method setMethod = classType.getMethod(setMethodName, new Classfield.getType();Method setMethod = classType.getMethod(setMethodName, field.getType();Object value = getMethod.invoke(object, new Object);System.out.println(fieldName + , + value);setMethod.invoke(objectOfCopy, new Objec

15、tvalue);return objectOfCopy;public static void main(String args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityExceptionProduct product = new Product(钢铁是怎样炼成的,34.9f);Product productOfCopy = (Product) new ref

16、lectTester().copy(product);System.out.println(Copy infomation: + , + productOfCopy.getName() + , +productOfCopy.getPrice();实验题 7 利用 Java 反射机制设计类 ArrayTester,创建一个 51015 的数组,并把索引位置为3510的元素的值设置为 59。实验结果:代码如下:package cn.edu.Array;import java.lang.reflect.Array;public class ter SuppressWarnings(unused)public

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号