为什么继承不能使用if语句

  • 本文关键字:if 语句 不能 继承 java
  • 更新时间 :
  • 英文 :


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

我有两个班,一个是学生班,另一个是儿童班(学生班(。

此代码不起作用(下面是代码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();
}
}

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

您的代码基本上是这样的:

if(...){
DormStudent student1 = new DormStudent();
}else{
Student student1 = new Student();
}
student1.printTotalCost();

在两个块中都声明CCD_ 1。每个块都有自己的范围。在这些块中声明的变量在外部是不可访问的。

问题不是找不到printTotalCost方法,而是最后一行的student1找不到。

快速修复:在if块之前声明学生:

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(); // do you really need to print it twice?
}
student1.printTotalCost();

您必须在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();

我看到您在理解范围方面遇到了一些问题。一个变量有一个特定的作用域,这意味着您只能从它所声明的作用域中访问它。

您可以找到这样的范围:查看变量上方的第一个{,并找到该变量的匹配}。这就是变量的作用域。

public class App { // start scope text
public void print(){
System.out.println(text);
}
String text = "hey";
}//end scope text right before the }

局部变量略有不同:在方法中声明的变量。这些只存在于创建它们的位置,直到顶部第一个{的}。

例如:

if(x < 5) {
String s = "Hi"; //scope String s starts here
System.out.println(s);
} //scope s ends just before this }
//if you call s here it no longer exists

还有一个更复杂的例子:

public class App {//start scope text
public void print(){
System.out.println(text);
int x = 5; //start scope int x
if(x < 10){
int y =23;//start scope y
System.out.println(y + x + text);
}//end scope y right before the }
} //end scope int x right before the }
String text = "hey";
}//end scope text right before the {

最新更新