这是我的第一个编程任务的测试类,我相信它可以正确测试 getter 和 setter,但我想知道是否有更好的方法使用 return 方法。我还是个新手,因为这是我的第一个编程任务。有这么多打印线感觉几乎不合适,但是有没有更好的返回方式? (至少对于 Java 初学者来说不是非常复杂的东西)

/**
   A class to test the Assignment class
*/
public class AssignmentTester
{

   /**
      Main method used to start program
      @param args the command line arguments for the program
   */
   public static void main(String[] args)
    {

      System.out.println("TESTING NO-ARGUMENT CONSTRUCTOR AND GETTERS \n=========================================== \n");

         Assignment noArgGetterTest = new Assignment();

               System.out.println(noArgGetterTest.getTitle() + "\nExpected: Assignment 1 \n");
               System.out.println(noArgGetterTest.getDueDate() + "\nExpected: 01/01/2019 \n");
               System.out.println(noArgGetterTest.getMaxPoints() + "\nExpected: 10.0 \n");
               System.out.println(noArgGetterTest.getCategory() + "\nExpected: Programming Assignments \n \n");

      System.out.println("Testing Setters \n=============== \n");

         Assignment setterTest = new Assignment();

               setterTest.setTitle("CodeLab 1");
               System.out.println(setterTest.getTitle() + "\nExpected: CodeLab 1 \n");

               setterTest.setDueDate("02/09/2019");
               System.out.println(setterTest.getDueDate() + "\nExpected: 02/09/2019 \n");

               setterTest.setMaxPoints(5.0);
               System.out.println(setterTest.getMaxPoints() + "\nExpected: 5.0 \n");

               setterTest.setCategory("CodeLab, Quizzes, ICE");
               System.out.println(setterTest.getCategory() + "\nExpected: CodeLab, Quizzes, ICE \n \n");

      System.out.println("Testing Argument Constructor and Getters \n======================================== \n");

         Assignment getterTest = new Assignment("Quiz 3.1", "03/13/2019", 2.0, "CodeLab, Quizzes, ICE");

               System.out.println(getterTest.getTitle() + "\nExpected: Quiz 3.1 \n");
               System.out.println(getterTest.getDueDate() + "\nExpected: 03/13/2019 \n");
               System.out.println(getterTest.getMaxPoints() + "\nExpected: 2.0 \n");
               System.out.println(getterTest.getCategory() + "\nExpected: CodeLab, Quizzes, ICE");
   }
}

我创建用于创建赋值的参数和参数的第一个类文件:
/**
   Describes an assignment's title, due date, total points value, and category
*/

public class Assignment
{
   private String title;     //Title of assignment
   private String dueDate;   //Due date of assignment
   private double maxPoints; //Max points of assignment
   private String category;  //Category of assignment

   /**
      Initialize instance variables for assignment project (no argument-constructor)
   */
   public Assignment()
   {
      title = "Assignment 1";
      dueDate = "01/01/2019";
      maxPoints = 10.0;
      category = "Programming Assignments";
   }

   /**
      Initialize instance variables for the assignment project (argument constructor)
      @param t title of assignment
      @param d due date of assignment
      @param m max points for the assignment
      @param c category of assignment
   */
   public Assignment(String t, String d, double m,String c)
   {
      title = t;
      dueDate = d;
      maxPoints = m;
      category = c;
   }

   /**
      Sets the value of title
      @param t title of assignment
   */
   public void setTitle(String t)
   {
      title = t;
   }

   /**
      Sets the value of dueDate
      @param d due date of assignment
   */
   public void setDueDate(String d)
   {
      dueDate = d;
   }

   /**
      Sets value of maxPoints
      @param m max points of assignment
   */
   public void setMaxPoints(double m)
   {
      maxPoints = m;
   }

   /**
      Sets the value of category
      @param c category of assignment
   */
   public void setCategory(String c)
   {
      category = c;
   }

   /**
      Returns the value of title
      @return title of assingment
   */
   public String getTitle()
   {
      return title;
   }

   /**
      Returns the value of dueDate
      @return due date of assignment
   */
   public String getDueDate()
   {
      return dueDate;
   }

   /**
      Returns the value of maxPoints
      @return max points of assignment
   */
   public double getMaxPoints()
   {
      return maxPoints;
   }

   /**
      Returns the value of category
      @return category of assingmen
   */
   public String getCategory()
   {
      return category;
   }
}

最佳答案

// I have done the same problem with using the return keyword, its awesome to use //return.
// but you should know "method" before using return keyword. Basically, you will know //later that the "method" helps you to save writing the same code. Cheers:)

public class AssignmentTester {

    public static Assignment noArgumentConstructorAndGetters() {
        System.out.println("TESTING NO-ARGUMENT CONSTRUCTOR AND GETTERS \n=========================================== \n");

        Assignment noArgGetterTest = new Assignment();

        return noArgGetterTest;

    }

    public static void printMethod(Assignment ass) {

        System.out.println(ass.getTitle());
        System.out.println(ass.getDueDate());
        System.out.println(ass.getMaxPoints());
        System.out.println(ass.getCategory());

    }

    public static Assignment testingSetters() {

        System.out.println("Testing Setters \n=============== \n");

        Assignment setterTest = new Assignment();

        setterTest.setTitle("CodeLab 1");

        setterTest.setDueDate("02/09/2019");

        setterTest.setMaxPoints(5.0);

        setterTest.setCategory("CodeLab, Quizzes, ICE");

        return setterTest;
    }

    public static Assignment testingArgumentsAndConstructors() {

        System.out.println("Testing Argument Constructor and Getters \n======================================== \n");

        Assignment getterTest = new Assignment("Quiz 3.1", "03/13/2019", 2.0, "CodeLab, Quizzes, ICE");

        return getterTest;


    }

    public static void main(String[] args) {
        printMethod(noArgumentConstructorAndGetters());
        printMethod(testingSetters());
        printMethod(testingArgumentsAndConstructors());


    }

}

关于java - 在这种情况下返回比 Sys.out.print 更好的选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54394993/

10-12 02:50