首页 应用 游戏 资讯 攻略

利用BlueJ对程序进行测试

时间:2011-04-26 关注公众号 来源:网络

  bluej 可以不写main函数,就对程序进行操作非常简单的测试。

  简单功能如何:

  首先,在以前,我们对自己所写的程序测试,需要如下操作:

利用BlueJ对程序进行测试

  在main函数中,有对各种对StuClass方法测试的代码。

  而如今,我们可以省去main函数的大量书写,通过另外一种方法更加快捷地对程序进行测试:

利用BlueJ对程序进行测试

  操作之后,在bluej界面左下角,出现:

利用BlueJ对程序进行测试

  红色显示的区域即为 类的 一个实例,右击之后,可以对其方法进行测试,如:void addStu(String name), 并且可以通过 Inspect 对实例的变量进行测试,观察。

  注意:

  1.private方法 不会显示出来,因为 类的对象不能对 类的private方法进行调用。

  如果构造函数被private修饰,则不能通过此方法进行测试,因为被private修饰后,只有类的内部可以使用。

  2.用static修饰的变量,不需要创建实例,而直接右键点击类,进行观察,因为在是类的变量。

  3.用static修饰的方法,同样不需要创建实例,直接右键点击类,可以进行调用,如果语句:Student.createStudent(name);因为是类的方法。

  下面,附上代码:

  Java代码

  public class StuClass { private Student[] stus; private int number; public StuClass() { stus = new Student[50]; number = 0; } public void addStu(String name) { stus[number] = Student.createStudent(name); number ++; } } public class Student { private String stuNum; private String name; private static int num = 0; public static Student createStudent(String name) { String stuNum; String numString; num ++; if (num < 10) numString = "00" + num; else if (num < 100) numString = "0" + num; else numString = "" + num; stuNum = "JB09" + numString; return new Student(stuNum, name); } private Student(String stuNum, String name) { this.stuNum = stuNum; this.name = name; } } //以下是课堂的笔记

  //1.stuNum should be created by CLASS_Student(it's okay that CLASS_StuClass arrange the stuNum, but stuNum is the attribute of student, it's better to create stuNum in CLASS_Student.)

  //2.avoid the mistake made by OBJECT_StuClass(if delete the method createStudent, then the constructor can be public, but if CLASS_StuClass' OBJECT have wrong operation, stuNum may wrong,too).

阅读全文
扫码关注“ 多特资源库
更多更全的软件资源下载
文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站)
玩家热搜

相关攻略

正在加载中
版权
版权说明

文章内容来源于网络,不代表本站立场,若侵犯到您的权益,可联系我们删除。(本站为非盈利性质网站)

电话:13918309914

QQ:1967830372

邮箱:rjfawu@163.com

toast