Java learning 14- Object type conversion (Casting)

The basic data types Casting:

  1. Automatic type conversion: smaller data types can be automatically converted to uppercase data types, such as long g = 20; double d = 12.0f

  2. cast: large data types can be cast (Casting) into small data types, such as float f = (float) 12.0; int a = (int) 1200L

Cast of Java objects called modeling

  1 can automatically convert from subclass to superclass type

  2. from the parent class must implement to convert the type of a subclass by molding (cast)

  3. reference no inheritance relationship between the type of conversion is illegal

E.g:

. 1  Package Day14;
 2  
. 3  public  class the Test {
 . 4      public  static  void main (String [] args) {
 . 5  //         int I = 10;
 . 6  //         Long L = I; // automatic type conversion, a small turn up
 . 7          
. 8  / /         Long L = 10L;
 . 9  //         int I = (int) L; // cast
 10          
. 11  //         Student Student new new S = ();
 12 is  //         the Person P = S; // from subclass to superclass autochanger type
 13 is          
14  //        P = the Person new new the Person ();
 15  //         ; S = Student (Student) P // from the parent class to the type of conversion subclass must molding (cast) to achieve
 16          
. 17  //         new new the Test Test T = ( );
 18 is  //         the Person P1 = T; // no conversion type inheritance is illegal 
. 19          
20 is      }
 21 }

Another example: String to automatically switch from the Object

. 1  Package Day14;
 2  
. 3  public  class the Test {
 . 4      public  static  void main (String [] args) {
 . 5          
. 6          // Object parent is the highest of all classes 
. 7          
. 8          String S = "Hello" ;
 . 9          Object obj = S; / / may be performed from subclass to superclass automatic type conversions 
10          System.out.println (obj);
 . 11      }
 12 is }

Print results: hello

And cast from Object to String

Package Day14; 

public  class the Test {
     public  static  void main (String [] args) { 
        
        // Object class is the highest of all the parent class 
        
//         String S = "Hello";
 //         Object obj = S; // from subclass to parent type conversion may be performed automatically
 //         System.out.println (obj); 
        
        Object obj = "Hello" ; 
        String S = (String) obj; // the type of conversion to parent subclass must molding (compulsory type conversion) to achieve 
        System.out.println (S); 
    } 
}

Print results: hello

 

Object Type Conversion Example: create test method in the Person class, creating class Student method getSchool

Package Day14; 

public  class the Person {
     public  void test () { 
        System.out.println ( "This is the person's test method" ); 
    } 
}

as well as

Package Day14; 

public  class Student the extends the Person {
     public  void getSchool () { 
        System.out.println ( "This is getSchool method of student" ); 
    } 
}

Then tested in the test class

Package Day14; 

public  class the Test {
     public  static  void main (String [] args) { 
                
        the Test T = new new the Test (); 
        
        t.method ( new new Person ()); // incoming method calls the method and Person object 
        
    } 
    
        public  void method (the Person e) {
             IF (e the instanceof Student) { // Analyzing e is not a Student object 
                Student S = (Student) e; 
                s.getSchool (); // if, getSchool Student's method calls 
            } the else {
                e.test (); // the other hand, the test method of calling Person 
            }     
    } 
}

Print result: This is the person of the test method.

Similarly, you can also pass the Student object

Package Day14; 

public  class the Test {
     public  static  void main (String [] args) { 

        the Test T = new new the Test (); 
        
//         t.method (new new Person ()); // incoming method calls the Person object and method 
        t. method ( new new Student ()); // incoming method calls the method object and Student 
    } 
    
        public  void method (the Person e) {
             IF (e the instanceof Student) { // Analyzing e is not a Student object 
                Student S = (Student) e ; 
                s.getSchool (); //If so, getSchool Student's method calls 
            } the else { 
                e.test (); // the other hand, the test method of calling Person 
            } 
    } 
}

Print result: This is getSchool method of student

 

to sum up:

  1. The higher the basic data types into a lower level of basic data types, called: cast

  2. The lower level of the basic data types into a higher level of basic data types, called: Automatic type conversion

  3. parent (eg: the Person) to a subclass (eg: Student), called: downward transition (judged by the instanceof)

  4. subclass (eg: Student) converting the parent (such as: Person), called: upcast

Guess you like

Origin www.cnblogs.com/su-peng/p/12449771.html