Reference type of conversion

Polymorphic transition into downcast and upcast two kinds:

 

Upcast:

In fact, the object handle assigned to the class type of reference to the parent class type, this process is automatic conversion, similar to the basic data types of automatic type conversion

 

format:

Parent class type variable name = new new subclass type (); 
eg: Animal A = new new Cat ();

 

 

Downcast:

Parent cast down to the subclass type process that is mandatory, similar to the basic data types of casts

 

format:

Type variable name = subclass (subclass type) parent class variable name; 
such as: Cat C = (Cat) A;  

 

Downcast once did not pay attention, there will be type cast exception: ClassCastException, so it should be before the transition to judge for themselves whether the transition

Code Example

Defined categories:

abstract class Animal {  
    abstract void eat();  
}  

class Cat extends Animal {  
    public void eat() {  
        System.out.println("吃鱼");  
    }  
    public void catchMouse() {  
        System.out.println("抓老鼠");  
    }  
}  

class Dog extends Animal {  
    public void eat() {  
        System.out.println("吃骨头");  
    }  
    public voidwatchHouse () {   
        System.out.println ( "housekeeping" );   
    }   
}

The transition process, believe it will encounter this problem, please see the following code:

public  class the Test {
     public  static  void main (String [] args) {
         // upcast   
        Animal A = new new Cat ();   
        a.eat ();                // call is a Cat EAT 

        // downcast   
        Dog D = ( Dog) a;        
        d.watchHouse ();         // call is given the Dog watchHouse [run] 
    }   
}

This code can be compiled, but the runtime, but reported out ClassCastException, cast abnormal! This is because, obviously creates an object of type Cat, run-time, of course, can not be converted into a Dog object. These two types do not have any inheritance, do not meet the definition of the type of conversion.

instanceof keyword

To avoid a ClassCastException, Java provides instanceofkeyword, to do the type of reference variable check the following format:

Variable names instanceof data type 
If the variable part of the data type, returns true. 
If the variable does not belong to the data type, returns false.

Therefore, before the conversion, we do a better determination, as follows:

public  class the Test {
     public  static  void main (String [] args) {
         // upcast   
        Animal A = new new Cat ();   
        a.eat ();                // call is a Cat EAT 

        // downcast   
        IF (A the instanceof Cat) { 
            Cat C = (Cat) a;        
            c.catchMouse ();         // call is a Cat catchMouse 
        } the else  IF (a the instanceof Dog) { 
            Dog D = (Dog) a;       
            d.watchHouse ();       // call is Dog's watchHouse 
        } 
    }   
}

 

Guess you like

Origin www.cnblogs.com/libinhong/p/10990532.html