利用BlueJ对程序进行测试

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

 

简单功能如何:

 

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

 

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

 

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

 

 

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

 

 

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

 

注意:

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

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

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

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

 

下面,附上代码:

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

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值