歡迎您光臨本站 註冊首頁

利用BlueJ對程序進行測試

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

bluej 可以不寫main函數,就對程序進行操作非常簡單的測試.

簡單功能如何:

首先,在以前,我們對自己所寫的程序測試,需要如下操作:

在main函數中,有對各種對StuClass方法測試的代碼.

而如今,我們可以省去main函數的大量書寫,通過另外一種方法更加快捷地對程序進行測試:

操作之後,在bluej界面左下角,出現:

紅色顯示的區域即為 類的 一個實例,右擊之後,可以對其方法進行測試,如:void addStu(String name), 並且可以通過 Inspect 對實例的變數進行測試,觀察.

注意:

1.private方法 不會顯示出來, 類的對象不能對 類的private方法進行調用.

如果構造函數被private修飾,則不能通過此方法進行測試,被private修飾后,只有類的內部可以使用.

2.用static修飾的變數,不需要創建實例,而直接右鍵點擊類,進行觀察,在是類的變數.

3.用static修飾的方法,同樣不需要創建實例,直接右鍵點擊類,可以進行調用,如果語句:Student.createStudent(name);是類的方法.

下面,附上代碼:

Java代碼

  1. public class StuClass
  2. {
  3. private Student[] stus;
  4. private int number;
  5. public StuClass()
  6. {
  7. stus = new Student[50];

  8. number = 0;
  9. }
  10. public void addStu(String name)
  11. {
  12. stus[number] = Student.createStudent(name);
  13. number ;
  14. }
  15. }
  16. public class Student
  17. {
  18. private String stuNum;
  19. private String name;
  20. private static int num = 0;
  21. public static Student createStudent(String name)
  22. {
  23. String stuNum;
  24. String numString;
  25. num ;
  26. if (num < 10) numString = "00" num;
  27. else if (num < 100) numString = "0" num;
  28. else numString = "" num;
  29. stuNum = "JB09" numString;
  30. return new

    Student(stuNum, name);
  31. }
  32. private Student(String stuNum, String name)
  33. {
  34. this.stuNum = stuNum;
  35. this.name = name;
  36. }
  37. }

//以下是課堂的筆記:

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


[火星人 ] 利用BlueJ對程序進行測試已經有376次圍觀

http://coctec.com/docs/java/show-post-60092.html