Post-course PPT 7

Source: Internet
Author: User

1.PPT P9

There are now three classes:

Class mammal{}//Parent

Class Dog extends mammal {}//child

Class Cat extends mammal{}//child

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? Which one 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.

1) Prior judgment

M=d is possible, indicating that I have defined a reference to a mammal type that points to the newly created object of the dog type, that is, the reference of the parent class to the object of its own child class. Because dog is inherited from its parent class mammal, references to mammal types are objects that can point to cat types: But D=m cannot, because the parent object reference cannot be assigned to a subclass object reference variable, except for a forced conversion, that is, a downward transition (parent Class) d= (Dog) m.

D=c can also cause compilation errors (comment out the d=m), the reference of the parent type must point to the object of the subclass, that is, to whom to convert, because the parent class refers to the object of the Cat class, and it is not possible to cast it to the dog class.

2):

2.PPT P11

1) The running result of the program is:

2) The result: the first direct output of the parent class, the value of the child, the second directly output the value of the subclass, the third is to assign the value of the class to the parent class, thus outputting the result as a subclass of the statement, the fourth sentence is the value++ of the parent, but the output is still a subclass of the statement, The reference is still a subclass of myvalue, so the output value is still 200, the fifth one is to convert the parent coercion type to child, and then the + + operation, the value is changed, the output is 201.

3) Summary: When the parent and child classes have the same method, calling that method is determined by the object's own actual type.

2.PPT P24

3.PPT P27

No, because the source program has a system exit statement, after the catch to the error, after running the corresponding statement, System.exit (0), so that the system exits directly, no longer execute the statement in finally.

4.

Hands-on brain:

Ø Write a program that asks 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 must be robust enough that no matter what content the user enters, it will not crash. Program code:

Package chengjipaiming;
Import Java.util.Scanner;
Class score
{
int score;
void Get ()
{

try{
Scanner a=new Scanner (system.in);
System.out.print ("Input score (0~100):");
Score=a.nextint ();
if (score<0| | SCORE>100)
{
System.out.println ("The number of inputs exceeds the defined range");
Get ();
}
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("Input error, check after input;");
Get ();
}
}
void Show ()
{

if (score>=0&&score<=60) System.out.print ("fail");
if (score>60&&score<=70) System.out.print ("Pass");
if (score>70&&score<=80) System.out.print ("Medium");
if (score>80&&score<=90) System.out.print ("Liang");
if (score>90&&score<=100) System.out.print ("excellent");
}
}
public class Range {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Score C=new score ();
C.get ();
C.show ();
}
}

Post-course PPT 7

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.