Polymorphism and abnormal handling hands-on brain

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.