When a program parent=child an object type that passes the parent class of the assignment statement to the child type, the method of the subclass is called when the method is called. However, when you use member variables, you use your own member variables.
Statement (child) parent. myvalue++, which is similar to forcing a type cast, forces a member of the parent class to be transformed into a member variable of a subclass, causing the subclass member variable to change, so the value changes when the subclass method is called.
Added code:
Parent parent1=new parent ();
Parent=parent1;
Parent.printvalue ();
Will output 100, but there is an error following coercion of type conversion, so casting must be based on the subclass object type when the parent class object is created.
Importjavax.swing.*; Public classProject { Public Static voidMain (String args[]) {string grade; Grade=joptionpane.showinputdialog ("Please enter student's score:"); while(true) { Try { intg=Integer.parseint (grade); if(g>=0&&g<60) {Joptionpane.showmessagedialog (NULL, "Failed", "results", Joptionpane.plain_message); Break; } Else if(g>=60&&g<70) {Joptionpane.showmessagedialog (NULL, "Pass", "results", Joptionpane.plain_message); Break; } Else if(g>=70&&g<80) {Joptionpane.showmessagedialog (NULL, "Zhong", "Results", Joptionpane.plain_message); Break; } Else if(g>=80&&g<90) {Joptionpane.showmessagedialog (NULL, "good", "results", Joptionpane.plain_message); Break; } Else if(g>=90&&g<=100) {Joptionpane.showmessagedialog (NULL, "excellent", "results", Joptionpane.plain_message); Break; } ElseGrade=joptionpane.showinputdialog ("Input error, please re-enter:"); } Catch(Exception e) {grade=joptionpane.showinputdialog ("Input error, please re-enter:"); } } }}
As shown in the code above
Try is to detect if the code in his range has an exception if there is a catch output exception result, and no program continues to execute.
The example also involves the finally, which is used to do the final processing, regardless of whether there is an exception occurred in the statement will be executed.
The try and catch used in the example above are all handled by the exception, but you can also define an exception class to detect an exception.
Polymorphism and abnormal handling hands-on brain