Summary of object type conversion

xiaoxiao2021-03-06  41

Look at the following example!

Import java.lang. *;

Public class j02090504

{

Public static void main (String Para [])

{

A Obj1;

System.out.Println ("A object reference A instance");

Obj1 =

NEW A ();

Obj1.printX ();

/ / ((B) Obj1) .test (); // Exception, Obj1 instance does not meet Class B, but will not find it during compilation period

System.out.println ("obj1.y =" obj1.y); // a defined Y

System.out.println ("============================");

System.out.println ("(a) b ==> a object reference B instance");

Obj1 = (a)

NEW B ());

/ / Usually do not need to do this transformation, because Obj1 = new b () can automatically transform

Obj1.printX (); // Evil is the PrintX () method of B.

//obj1.test (); // Compiling errors, Class A does not have test () method

System.out.println ("obj1.y =" obj1.y);

System.out.println ("The object of the class A is resected to B ...");

((B) Obj1) .printX ();

/ / Evil is the PrintX () method of B. The former is the same, no need to do this.

((B) Obj1). Test (); // There is a test () method

System.out.println ("(b) Obj1) .y =" ((b) Obj1) .y);

System.out.println ("============================");

// b Testobj = (b) new a (); // Execute the exception, the example of the Class C is not satisfied

System.out.println ("(b) ((a) b) ==> B Autographs"); // B object instance

B Obj2 = (b) OBJ1;

// OBJ1 is currently hereby an instance of the B class, and its object reference can be converted to Class B

Obj2.printX ();

Obj2.test ();

System.out.println ("Obj2.y =" obj2.y); // class B definition Y

System.out.println ("The object of the class B is converted to a ...");

((A) Obj2) .printX (); // Evil or B's printX () method implementation

/ / ((A) Obj2) .test (); // Compiling errors, Class A does not test () method

System.out.println ("((a) obj2) .y =" ((a) Obj2) .y); // Class A definition of Y

}

}

Class A

{

INT x = 199;

Int y = 333;

Public void printX ()

{

System.out.println ("x 100 =" (x 100));

}

}

Class B Extends A

{

Double X = 255.51;

Double y = 11.23;

Public void printX ()

{

System.out.println ("x =" x);

}

Public void test ()

{

System.out.println ("TEST () in b");

}

}

The experience of the results after reading it is:

1, transformation, but can call this reference to the object to be the most primitive new

Which class is that a pair is.

2, can not transform the instance of the parent class into subclasses? Because the parent class is not full of foot classes.

The subclass can be transformed into a parent class because the subclass has the structure of the parent class. At this time, the reference to the parent class can be referenced.

Subclass variables (because Override !!!!).

Finally, I summed it. The problem is to appear on an instance in the type of creation!

We can see the entire main program, a total of two statements that create new categories twice, see the colored parts NEW statements.

Concluded as follow:

First, if the object comes out is a parent class type, it cannot turn the parent class type to subclass type.

Second, if the object comes out is a subclass type, it can be converted to a parent class. And this reference

The variable taking is a subclass definition because it is Override. But the quotation of the parent class is not

The reason for the error is that the parent class does not have such a variable, don't forget that we turn the subclass into the parent class. In this way, this situation is very clear.

(Ie the use of subclasses as the parent class. And the child is beyond the parent class. Oh, this is the essence of the parent class reference reference subclass example, but just a word!)

Such a parent class type can be converted to a subclass type, but it is only the status of the subclasses back to the original.

So far, the case of type conversion is one clear.

转载请注明原文地址:https://www.9cbs.com/read-68531.html

New Post(0)