首页 > 解决方案 > 为什么继承不适用于 if 语句

问题描述

我必须根据不同类型的学生计算学生成本。学生有两种类型,普通学生和宿舍学生。与普通学生相比,宿舍学生将获得更多信息。我有两个代码,一个是工作,另一个不是。我不知道为什么它不起作用。

我有两个班级,一个是学生班级和 DormStudent 班级,这是孩子班级(学生班级)。

此代码不起作用(以下是代码 1)

    public static void main(String[] args) {
    String selectType = "Are you Dorm (all other interger) or Communiting (1):";
    int inputSelection=checkInputInteger(selectType);
    String inputName = checkInput("Enter your name:");
    int inputYear = checkInputInteger("Enter Year of Entrance:");
    double inputSuppliesCost = checkInputDouble("Enter Supplier cost per year:");
    int inputCreditHour = checkInputInteger("Enter class hour per year:");
    double inputHourPrice= checkInputDouble("Enter the cost class per hour:");


    if(inputSelection==1){
        int inputDurationWeek= checkInputInteger("Enter the number of week stay:");
        double inputWeeklyExpenses = checkInputDouble("Enter weekly expeneses");
        double inputCostRoom =checkInputDouble("Enter the cost of the room or board per week");
        DormStudent student1 = new DormStudent(inputName,inputYear,inputSuppliesCost,inputCreditHour,inputHourPrice,inputDurationWeek,inputWeeklyExpenses,inputCostRoom);

    }else{
        Student student1 = new Student(inputName, inputYear, inputSuppliesCost,inputCreditHour, inputHourPrice);

    }
    student1.printTotalCost();
}

我有错误说:找不到符号 student1.printTotalCost() (student1) 但是当我尝试这是工作时(下面是代码 2)。

public static void main(String[] args) {
    String selectType = "Are you Dorm (all other interger) or Communiting (1):";
    int inputSelection=checkInputInteger(selectType);
    String inputName = checkInput("Enter your name:");
    int inputYear = checkInputInteger("Enter Year of Entrance:");
    double inputSuppliesCost = checkInputDouble("Enter Supplier cost per year:");
    int inputCreditHour = checkInputInteger("Enter class hour per year:");
    double inputHourPrice= checkInputDouble("Enter the cost class per hour:");


    if(inputSelection==1){
        int inputDurationWeek= checkInputInteger("Enter the number of week stay:");
        double inputWeeklyExpenses = checkInputDouble("Enter weekly expeneses");
        double inputCostRoom =checkInputDouble("Enter the cost of the room or board per week");
        DormStudent student1 = new DormStudent(inputName,inputYear,inputSuppliesCost,inputCreditHour,inputHourPrice,inputDurationWeek,inputWeeklyExpenses,inputCostRoom);
        student1.printTotalCost();
    }else{
        Student student1 = new Student(inputName, inputYear, inputSuppliesCost,inputCreditHour, inputHourPrice);
        student1.printTotalCost();
    }
}

我只是不明白为什么在代码 1 中的 if 语句之后无法识别 student1。这是否意味着类初始化只能在 if 语句中工作?

标签: java

解决方案


您必须在语句上方if声明一个具有更抽象类型的单个student1变量(在这里,Student我假设),并在每个分支中将其与实际对象一起分配。

  Student student1;
  if(inputSelection==1){
      int inputDurationWeek= checkInputInteger("Enter the number of week stay:");
      double inputWeeklyExpenses = checkInputDouble("Enter weekly expeneses");
      double inputCostRoom =checkInputDouble("Enter the cost of the room or board per week");
      student1 = new DormStudent(inputName,inputYear,inputSuppliesCost,inputCreditHour,inputHourPrice,inputDurationWeek,inputWeeklyExpenses,inputCostRoom);
      student1.printTotalCost();
  }else{
      student1 = new Student(inputName, inputYear, inputSuppliesCost,inputCreditHour, inputHourPrice);
      student1.printTotalCost();
  }
  student1.printTotalCost();

推荐阅读