java - 验证子类构造函数中的字段的问题

标签 java validation inheritance constructor

我现在正在学习Java的继承。我总共有3个问题,谢谢大家的支持。

第一个问题:我可以在类的构造函数中验证类的字段吗?

第二个问题:有些人建议我抛出异常进行验证。这是否意味着向调用者方法抛出异常,或者抛出异常并在构造函数内处理它?<​​/p>

第三个问题:假设该类不是子类,我可以验证代码中显示的字段,而不是使用异常吗?(假设代码不会因为 super() 而产生错误)。

子类

import javax.swing.JOptionPane;

public class Essay extends GradedActivity
{
   private final double MAXGRAMMAR = 30; 
   private final double MAXSPELLING = 20;   
   private final double MAXLENGTH = 20;
   private final double MAXCONTENT = 30;   

   private double grammar;
   private double spelling;
   private double length;
   private double content;


public Essay(double grammar, double spelling, double length, double content)

  {
      double total;

  while(grammar < 0 || grammar > MAXGRAMMAR)
  {
     grammar = Double.parseDouble(JOptionPane.showInputDialog(null, "Invalid grammar value, try again: "));
     this.grammar = grammar;
  }
  while(spelling < 0 || spelling > MAXSPELLING)
  {
     spelling = Double.parseDouble(JOptionPane.showInputDialog(null, "Invalid spelling value, try again: "));       
     this.spelling = spelling;
  }
  while(length < 0 || length > MAXLENGTH)
  {
     length = Double.parseDouble(JOptionPane.showInputDialog(null, "Invalid length value, try again: "));      
     this.length = length;

  }
  while(content < 0 || content > MAXCONTENT)
  {
     content = Double.parseDouble(JOptionPane.showInputDialog(null, "Invalid content value, try again: "));      
     this.content = content;
  }

  total = grammar + spelling + length + content;
  super(total);               
   }
};

父类(super class)

/**
   A class that holds a grade for a graded activity.
*/

public class GradedActivity
{
   private double score;  // Numeric score

   /**
      The setScore method sets the score field.
      @param s The value to store in score.
   */

   public void setScore(double s)
   {
      score = s;
   }

   /**
      The getScore method returns the score.
      @return The value stored in the score field.
   */

   public double getScore()
   {
      return score;
   }

   /**
      The getGrade method returns a letter grade
      determined from the score field.
      @return The letter grade.
   */

   public char getGrade()
   {
      char letterGrade;

      if (score >= 90)
         letterGrade = 'A';
      else if (score >= 80)
         letterGrade = 'B';
      else if (score >= 70)
         letterGrade = 'C';
      else if (score >= 60)
         letterGrade = 'D';
      else
         letterGrade = 'F';

      return letterGrade;
   }
}

最佳答案

  1. 是的,您可以在构造函数中进行验证。
  2. 您可以在这里做两件事。要么不要让用户通过抛出异常来创建具有无效值的对象,并让调用者处理异常,要么在提供错误输入时定义默认值。在某些情况下,第二种选择可能有效。抛出异常并在那里处理它没有多大意义,这几乎就像检查条件并在 else block 中处理它一样。
  3. 没有什么可以阻止您在代码中执行的操作,但作为良好的编码实践,用户输入是 View 的一部分,您创建一个 View ,允许您输入所需的值并对表单进行验证,一旦所有字段都有效,将其传递给构造函数。这样,构造函数就不会为每个无效条目提供一次性输入对话框,而只会产生烦恼因素。

关于java - 验证子类构造函数中的字段的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48273166/

相关文章:

java - 使用 AND 或 OR 条件手动构建 SQL 查询时出现 MySql 语法错误异常

java - 本例中如何使用 SwingWorker

java - 无法调用方法?

javascript - 在 JavaScript 中,如何确保数组至少有一个特定元素,而其他元素满足另一个条件?

grails - Angular JS和Grails REST服务

C# 泛型类定义与 inerited 类语法错误

java - 使用流生成 short[]

php - Laravel 字符串验证以允许空字符串

html - 目前,元素视频上不允许使用属性类型。如何解决这个错误?

database - 类图是否显示数据库结构?