java - 为什么继承不适用于 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 语句中工作?
解决方案
您必须在语句上方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();
推荐阅读
- node.js - React 应用程序无法加载 localhost:3000
- visual-studio - 无法从 VS2017 加载模板到 VS2019
- python - Python Scrapy Crawler - 尝试在 Github 上自动记录问题不起作用 - Gist 无法在 Linux 机器上创建(适用于 Windows)
- qt - 将 CMake 项目导入现有 QT 项目 .pro
- postgresql - Spring Batch - 多步执行模型,如果第 3 步失败,则第 1 步和第 2 步插入的数据也应该回滚
- apache-flink - Apache Flink 中的不可序列化对象
- java - Apache POI 获取字体指标
- javascript - 来自 puppeteer 文档的代码在 UnhandledPromiseRejectionWarning 之前不起作用
- java - 如何测试副屏?
- javascript - Javascript - 在多个文件中存储类方法?