利用BlueJ对程序进行测试


  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).