简体   繁体   English

从构造函数中调用时,关键字“ super”如何工作?

[英]How does the keyword “super” work, when called from a constructor?

import java.io.*;

class MyException1
{
static String str="";

 public static void main(String args[])
 {
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Enter your food");


 try{
 str=br.readLine();
 }catch(IOException e){
 System.out.println("Exception has been occurred"+e);
 }


 try{
 checkFood();
 }catch(BadException be){
 System.out.println("Exception"+be);
 }
 }


 private static void checkFood() throws BadException
 {

 if(str.equals("Rotten")|| str.equals("")){
 System.out.println("Bad food");
 //throw new BadException();
 throw new BadException("Not Eatable");
 }else{
 System.out.println("Good food !! enjoy");

 }
 }
}



class BadException extends Exception
{
String food;

 BadException()
 {
 super(); 
 food="invalid";
 System.out.println(food);
 }

 BadException(String s)
 {
 super(s); 
 food=s;
 }

 public String getError()
 {
 return food;
 }

}

In the program, how is it that this public String getError() returns the food variable? 在程序中,此public String getError()返回food变量是怎么回事? I have not called it anywhere? 我没有在任何地方打电话吗?

If I remove the line super(s); 如果我删除super(s);super(s); , then "Not Eatable" does not get printed. ,则不会打印“不可食用”。 But if I leave that line in, then it does get printed out. 但是,如果我将该行留在里面,那么它的确会打印出来。 How does this program flow work? 该程序流程如何工作?

If I remove the line super(s);, then "Not Eatable" does not get printed. 如果删除行超级;则不会打印“不可食用”。 But if I leave that line in, then it does get printed out. 但是,如果我将该行留在里面,那么它的确会打印出来。 How does this program flow work? 该程序流程如何工作?

super(s) will call the "super class" constructor that takes a string. super(s)将调用采用字符串的“ super class”构造函数。 Just like if you had called new Exception("Not Eatable") . 就像您调用过new Exception("Not Eatable") This constructor for Exception adds a message to the exception, so when you print it out, it will contain that text. Exception构造函数会向该Exception添加一条消息,因此当您将其打印出来时,它将包含该文本。

This has nothing to do with the variable food . 这与food种类无关。 You could remove the line food=s; 您可以删除food=s;这一行food=s; , and the message would still print out correctly. ,该消息仍将正确打印出来。

See this tutorial on the keyword super : 请参阅有关关键字super本教程:

http://download.oracle.com/javase/tutorial/java/IandI/super.html http://download.oracle.com/javase/tutorial/java/IandI/super.html

If you're still confused about how super works, then think about this. 如果您仍然对super工作原理感到困惑,请考虑一下。 You can recode BadException with this code, and your program will still do exactly the same thing: 您可以使用以下代码对BadException进行重新编码,而您的程序仍将执行完全相同的操作:

class BadException extends Exception
{
 BadException(String s)
 {
  super(s);
 }
}

This will also do the same thing: 这也将做同样的事情:

class Test extends Throwable
{
 String message;
 Test(String msg)
 {
  message = msg;
 }
 public String toString() {
  return "BadException: " + message;
 }
}

class BadException extends Test
{
 BadException(String s)
 {
  super(s);
 }
}

When you throw new BadException("not eatable"); 当您throw new BadException("not eatable"); you're instantiating a new BadException, which sets its member variable food to the string "not eatable". 您正在实例化一个新的BadException,它将其成员变量food设置为字符串“ not eatable”。 Then the call to getError() will return that string. 然后,对getError()的调用将返回该字符串。

It would be better style to to get rid of the food member variable and just make a call to super(String) since there is a constructor Exception(String message) 最好删除食物成员变量,然后调用super(String),因为有构造函数Exception(String message)

"super(s);" “超级”;” calls the super-class's constructor that takes one String. 调用采用一个String的超类的构造函数。 That is the Exception class. 那是Exception类。

If you take out "super(s);", then the compiler will implicitly put a "super();" 如果您删除“ super(s);”,则编译器将隐式放置“ super();”。 call in there, because the super class has a default constructor. 在那里调用,因为超类具有默认构造函数。 (That's why it's called a default constructor -- because it will get called by default if you don't specify anything else!) (这就是为什么它被称为默认构造函数的原因-因为如果您不指定其他任何内容,它将在默认情况下被调用!)

Since that's the same as calling "super(null);", the message (which is in the variable "s") doesn't get passed up to the super class, and thus it isn't there to print out .. 由于这与调用“ super(null);”相同,因此消息(位于变量“ s”中)不会传递给超类,因此该消息无法打印出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM