Multi-state and abnormal treatment of hands-on brain

Source: Internet
Author: User

Hands-on experiment one: Which of the following statements will cause a compilation error ? Why ? which will cause a run-time error ? Why ?

M=d;

D=m;

D= (Dog) m;

D=c;

C= (Cat) m;

First make a self-judgment, draw a conclusion, run the Testcast.java instance code to see if your judgment is correct:

The base class object in Java cannot be used as a subclass object, it needs to be implemented by casting, subclass object variable = ( subclass name ) base class object name; Error code is d=m; D=c;

Error reason: Type mismatch: cannot convert from mammal to Dog.

Hands-on experiment two: run the following test code

What is the result of the program running above? . How would you explain that you would get such an output? The computer is not wrong, the reason why you get such a result is also a reason, then from these running results, you can summarize the Java syntax features? Make sure to summarize the brain, then modify or write some code to test, verify your own ideas, and finally look at the following PPT to give the conclusion.

If the child class has the same field as the parent class , The fields in the subclass are substituted or hidden from the fields of the parent class, and the fields in the subclass are accessed (not the fields in the parent class). If the subclass method does want to access a field that is hidden in the parent class with the same name, it can be accessed with the super keyword.

If the subclass is used as a parent class , the field accessed through the subclass is the parent class !

The first value calls the method of the parent class, and the output value is

The second value calls the method of the subclass, and the output value is

The third neutron class is assigned to the parent class, so the method of the subclass is called, and the output value is

The constructor method for the subclass is called in the fourth, but the value is four, so there is no effect

The fifth is called a method of a subclass, and value is also a subclass of a class.

Rewrite one:

public class Parentchildtest {

public static void Main (string[] args) {

Parent parent=new parent ();

Parent.printvalue ();

Child child=new Child ();

Child.printvalue ();

Parent=child;

Parent.printvalue ();

parent.myvalue++;

System.out.println (Parent.myvalue);

System.out.println (Child.myvalue);

Parent.printvalue ();

/* ((Child) parent). myvalue++;

System.out.println (Parent.myvalue);

System.out.println (Child.myvalue);

Parent.printvalue (); */

}

}

Class parent{

public int myvalue=100;

public void Printvalue () {

System.out.println ("Parent.printvalue (), myvalue=" +myvalue);

}

}

Class Child extends parent{

public int myvalue=200;

public void Printvalue () {

System.out.println ("Child.printvalue (), myvalue=" +myvalue);

}

}

Run:

Rewrite two:

public class Parentchildtest {

public static void Main (string[] args) {

Parent parent=new parent ();

Parent.printvalue ();

Child child=new Child ();

Child.printvalue ();

Parent=child;

Parent.printvalue ();

/*parent.myvalue++;

System.out.println (Parent.myvalue);

System.out.println (Child.myvalue);

Parent.printvalue (); */

((child) parent). myvalue++;

System.out.println (Parent.myvalue);

System.out.println (Child.myvalue);

Parent.printvalue ();

}

}

Class parent{

public int myvalue=100;

public void Printvalue () {

System.out.println ("Parent.printvalue (), myvalue=" +myvalue);

}

}

Class Child extends parent{

public int myvalue=200;

public void Printvalue () {

System.out.println ("Child.printvalue (), myvalue=" +myvalue);

}

}

Run 2:

hands-on experiment three: please read and run Aboutexception.java example, and then learn the basics of implementing exception Handling in Java through the next few pages of PPT.

four hands: When there are multiple nested try...catch...finally , pay special attention to finally time of execution.

Read the Embedfinally.java example before you run it, observe its output, and summarize it.

Special attention:

When there are multiple layers of nested finally , exceptions are thrown at different levels and thrown at different locations, which can result in a different order of the finally statement block execution.

1), thefinally statement block may execute only if the try statement block corresponding to finally is executed .

2), executes the system.exit (0) statement in the try statement block , terminates the runtime of the Java virtual machine.

3), when a thread is interrupted when executing a try statement block or a catch statement block (interrupted ) or is terminated (killed), the corresponding finally statement block may not be executed.

4), more extreme situation, is the thread running a try statement block or catch statement block, suddenly crashes or power outages,finally The statement block will definitely not execute.

5), regardlessTryWhether the statement block ends normally or ends abnormally,finallystatement blocks are guaranteed to be executed. IfTrythe statement block ends normally, then theTryafter the statements in the statement block are executed, thefinallystatement block. IfTryThere are control transfer statements (return, Break,Continue)finallystatement block inTryorCatchin the statement blockreturnstatement before the execution.

The action of brain Five: discrimination: finally is the statement block bound to execute?

Please answer these questions through the Systemexitandfinally.java sample program

Executes the System.exit (0) statement in the TRY statement block, terminating the runtime of the Java virtual machine

The program to validate

Catchwho.java

Catchwho2.java

Embededfinally.java

Systemexitandfinally.java

Printexpressionstack.java

Throwmultiexceptionsdemo.java

When a method declaration throws more than one exception, the program runs smoothly as soon as one of the exceptions is catch in this method body .

Overridethrows.java

"The exception that is thrown by the throws clause of a subclass cannot be the parent of an exception object that is thrown by a method with the same name as its base class "

Exceptionlinkinrealworld.java

Multi-state and abnormal treatment of 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.