Java Action Brain 07

Source: Internet
Author: User

(1) There are now three classes :

Class mammal{}

Class Dog extends Mammal {}

Class Cat extends mammal{}

Three variables are defined and initialized for each class

Mammal M=null;

Dog D=new Dog ();

Cat c=new Cat ();

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;

Compile error

D=m;d=c;

An incorrect subclass object can be directly assigned to a base class variable. To assign a base class object to a Subclass object variable , You must perform a type conversion ,
Its syntax is : sub-class object variable = ( subclass name ) base class object name ;

Run error c= (Cat) m incorrect conversion confusion. If the type conversion fails , Java throws the following exception : ClassCastException

(2)

1. What is the result of the program running below?

2. How do you explain that you will get such output?

3. The computer is not wrong, the reason why you get such a running result is also a reason, then from these running results, you can summarize the Java syntax features?

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++;

Parent.printvalue ();

((child) parent). 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);

}

}

Operation Result:

Cause Analysis:

When a subclass has the same method as the parent class, and a parent class variable refers to a subclass object, which method is called, determined by the object's own "true" type, that is, the object is a subtype, it calls the method of the subtype, it is the parent type, and it invokes the method of the parent type. 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 !

(3)

Hands-on brain: Read and run the aboutexception.java sample, then learn java from the next few pages of PPT The basics of implementing exception handling in

(4)

Read the following code (Catchwho.java) to write out the program run results

Write The results of the Catchwho2.java program run:

When there are multiple nested try... Catch ... .. finally , pay special attention to finally time of execution.

Please read the Embedfinally.java Example before running it, observing its output and summarizing it.

Summary: 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 finally statement block execution.

(5)

Analysis: Thefinally statement block will be executed?

Please answer these questions through the systemexitandfinally.java sample Program

So the code in the finally block is bound to execute

(6)

When an exception occurs in the program, theJVM looks for the error handler in order of the method invocation.

You can use the printstacktrace and getMessage methods to understand what happens when an exception occurs:

Printstacktrace: Prints the method call stack.

Each object of the Throwable class has a getMessage method that returns a string that is in the Exception In a constructor, it is common for this string to contain information about a particular exception.

Please use the printexpressionstack.java sample to learn the above

(7)

A method can declare multiple exceptions to be thrown

int g (float h) throws Oneexception,twoexception

{ ... }

The Throwmultiexceptionsdemo.java example shows the relevant features

(8)

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

The Overridethrows.java example shows This syntax feature of Java

(9)

In practical development, it is often necessary to convert a specific "over-specialized" exception into a "business" exception and then capture and process it at the caller.

The Exceptionlinkinrealworld.java example shows a typical exception handling code template

(10)

Write a program that requires the user to enter an integer at run time, representing the exam results of a course, followed by a "fail", "Pass", "Medium", "good", "excellent" conclusion.

Requires that the program be robust enough that it does not crash regardless of what the user enters.

Source:

Import javax.swing.*;

public class Chengji {

public static void Main (string[] args) {

int a=0;

try{

String str=joptionpane.showinputdialog ("Please enter a score");

A=integer.parseint (str);

if (0<=a&&a<60)

{

Joptionpane.showmessagedialog (NULL, "Failed", "Result",

Joptionpane.plain_message);

}

else if (60<=a&&a<70)

{

Joptionpane.showmessagedialog (NULL, "Pass", "Result",

Joptionpane.plain_message);

}

else if (70<=a&&a<80)

{

Joptionpane.showmessagedialog (NULL, "Medium", "Result",

Joptionpane.plain_message);

}

else if (80<=a&&a<90)

{

Joptionpane.showmessagedialog (null, "good", "Result",

Joptionpane.plain_message);

}

else if (90<=a&&a<100)

{

Joptionpane.showmessagedialog (NULL, "excellent", "Result",

Joptionpane.plain_message);

}

Else

{

Joptionpane.showmessagedialog (NULL, "Input out of range", "Result",

Joptionpane.plain_message);

}

}

catch (Exception e) {

Joptionpane.showmessagedialog (NULL, "Input error", "Result",

Joptionpane.plain_message);

}

}

}

Operation Result:

Java Action Brain 07

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.